diff options
Diffstat (limited to 'cps-service')
5 files changed, 103 insertions, 8 deletions
diff --git a/cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java b/cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java index bbfe496d85..304ed288f5 100644 --- a/cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java +++ b/cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java @@ -188,4 +188,9 @@ public interface CpsModuleService { final Map<String, String> parentAttributes, final Map<String, String> childAttributes); + /** + * Remove any Yang Resource Modules from the DB that are no longer referenced by any schema set. + */ + void deleteUnusedYangResourceModules(); + } diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java b/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java index 4063a7f769..9f3f2cc571 100644 --- a/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java +++ b/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java @@ -106,7 +106,6 @@ public class CpsModuleServiceImpl implements CpsModuleService { } cpsModulePersistenceService.deleteSchemaSet(dataspaceName, schemaSetName); yangTextSchemaSourceSetCache.removeFromCache(dataspaceName, schemaSetName); - cpsModulePersistenceService.deleteUnusedYangResourceModules(); } @Override @@ -119,7 +118,6 @@ public class CpsModuleServiceImpl implements CpsModuleService { .stream().map(Anchor::getName).collect(Collectors.toSet()); cpsAnchorService.deleteAnchors(dataspaceName, anchorNames); cpsModulePersistenceService.deleteSchemaSets(dataspaceName, schemaSetNames); - cpsModulePersistenceService.deleteUnusedYangResourceModules(); for (final String schemaSetName : schemaSetNames) { yangTextSchemaSourceSetCache.removeFromCache(dataspaceName, schemaSetName); } @@ -182,6 +180,11 @@ public class CpsModuleServiceImpl implements CpsModuleService { childAttributes); } + @Override + public void deleteUnusedYangResourceModules() { + cpsModulePersistenceService.deleteUnusedYangResourceModules(); + } + private boolean isCascadeDeleteProhibited(final CascadeDeleteAllowed cascadeDeleteAllowed) { return CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED == cascadeDeleteAllowed; } diff --git a/cps-service/src/main/java/org/onap/cps/init/DbCleaner.java b/cps-service/src/main/java/org/onap/cps/init/DbCleaner.java new file mode 100644 index 0000000000..6bd3e1f204 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/init/DbCleaner.java @@ -0,0 +1,48 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.init; + +import java.util.concurrent.TimeUnit; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.api.CpsModuleService; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +@Slf4j +@RequiredArgsConstructor +@Service +public class DbCleaner { + + private final CpsModuleService cpsModuleService; + + /** + * This method will clean up the db during application start up. + * It wil run once and currently only removes unused yang resource modules. + * + */ + @Scheduled(initialDelay = 1, timeUnit = TimeUnit.SECONDS) + public void cleanDbAtStartUp() { + log.info("CPS Application started, commencing DB clean up"); + cpsModuleService.deleteUnusedYangResourceModules(); + log.info("DB clean up completed"); + } +} diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy index 1831506563..c02b06fd80 100644 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy @@ -142,8 +142,6 @@ class CpsModuleServiceImplSpec extends Specification { 1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset') and: 'schema set will be removed from the cache' 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset') - and: 'orphan yang resources are deleted' - 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules() and: 'the CpsValidator is called on the dataspaceName and schemaSetName' 1 * mockCpsValidator.validateNameCharacters('my-dataspace', _) where: 'following parameters are used' @@ -161,8 +159,6 @@ class CpsModuleServiceImplSpec extends Specification { 1 * mockCpsModulePersistenceService.deleteSchemaSet('my-dataspace', 'my-schemaset') and: 'schema set will be removed from the cache' 1 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', 'my-schemaset') - and: 'orphan yang resources are deleted' - 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules() and: 'the CpsValidator is called on the dataspaceName and schemaSetName' 1 * mockCpsValidator.validateNameCharacters('my-dataspace', 'my-schemaset') } @@ -187,8 +183,6 @@ class CpsModuleServiceImplSpec extends Specification { mockCpsModulePersistenceService.deleteSchemaSets('my-dataspace', _) and: 'schema sets will be removed from the cache' 2 * mockYangTextSchemaSourceSetCache.removeFromCache('my-dataspace', _) - and: 'orphan yang resources are deleted' - 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules() and: 'the CpsValidator is called on the dataspaceName' 1 * mockCpsValidator.validateNameCharacters('my-dataspace') and: 'the CpsValidator is called on the schemaSetNames' @@ -276,6 +270,13 @@ class CpsModuleServiceImplSpec extends Specification { 1 * mockCpsValidator.validateNameCharacters('some-dataspace-name', 'some-anchor-name') } + def 'Delete unused yang resource modules.'() { + when: 'deleting unused yang resource modules' + objectUnderTest.deleteUnusedYangResourceModules() + then: 'it is delegated to the module persistence service' + 1 * mockCpsModulePersistenceService.deleteUnusedYangResourceModules() + } + def getModuleReferences() { return [new ModuleReference('some module name','some revision name')] } diff --git a/cps-service/src/test/groovy/org/onap/cps/init/DbCleanerSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/init/DbCleanerSpec.groovy new file mode 100644 index 0000000000..5106d29fa5 --- /dev/null +++ b/cps-service/src/test/groovy/org/onap/cps/init/DbCleanerSpec.groovy @@ -0,0 +1,38 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.init + +import org.onap.cps.api.CpsModuleService +import spock.lang.Specification + +class DbCleanerSpec extends Specification { + + def mockCpsModuleService = Mock(CpsModuleService) + + def objectUnderTest = new DbCleaner(mockCpsModuleService) + + def 'DB clean up.'() { + when: 'scheduled method is triggered' + objectUnderTest.cleanDbAtStartUp() + then: 'the unused yang resource modules are deleted' + 1 * mockCpsModuleService.deleteUnusedYangResourceModules() + } +} |