/*
* ============LICENSE_START=======================================================
- * Copyright (C) 2022-2023 Nordix Foundation
+ * Copyright (C) 2022-2024 Nordix Foundation
* Modifications Copyright (C) 2022 Bell Canada
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
package org.onap.cps.ncmp.api.impl.inventory.sync
+import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.MODULE_SYNC_FAILED
+import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.MODULE_UPGRADE_FAILED
+
import ch.qos.logback.classic.Level
import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.spi.ILoggingEvent
import com.hazelcast.config.Config
import com.hazelcast.instance.impl.HazelcastInstanceFactory
import com.hazelcast.map.IMap
-import org.junit.jupiter.api.AfterEach
-import org.junit.jupiter.api.BeforeEach
import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler
-import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleSyncService
-import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleSyncTasks
-import org.onap.cps.ncmp.api.impl.inventory.sync.SyncUtils
import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
import org.onap.cps.ncmp.api.impl.inventory.CompositeState
def logger = Spy(ListAppender<ILoggingEvent>)
- @BeforeEach
void setup() {
- ((Logger) LoggerFactory.getLogger(ModuleSyncTasks.class)).addAppender(logger);
- logger.start();
+ ((Logger) LoggerFactory.getLogger(ModuleSyncTasks.class)).addAppender(logger)
+ logger.start()
}
- @AfterEach
- void teardown() {
- ((Logger) LoggerFactory.getLogger(ModuleSyncTasks.class)).detachAndStopAllAppenders();
+ void cleanup() {
+ ((Logger) LoggerFactory.getLogger(ModuleSyncTasks.class)).detachAndStopAllAppenders()
}
def mockInventoryPersistence = Mock(InventoryPersistence)
- def mockSyncUtils = Mock(SyncUtils)
+ def mockSyncUtils = Mock(ModuleOperationsUtils)
def mockModuleSyncService = Mock(ModuleSyncService)
def 'Module Sync ADVISED cm handles.'() {
given: 'cm handles in an ADVISED state'
- def cmHandle1 = advisedCmHandleAsDataNode('cm-handle-1')
- def cmHandle2 = advisedCmHandleAsDataNode('cm-handle-2')
+ def cmHandle1 = cmHandleAsDataNodeByIdAndState('cm-handle-1', CmHandleState.ADVISED)
+ def cmHandle2 = cmHandleAsDataNodeByIdAndState('cm-handle-2', CmHandleState.ADVISED)
and: 'the inventory persistence cm handle returns a ADVISED state for the any handle'
mockInventoryPersistence.getCmHandleState(_) >> new CompositeState(cmHandleState: CmHandleState.ADVISED)
when: 'module sync poll is executed'
1 * mockModuleSyncService.deleteSchemaSetIfExists('cm-handle-1')
1 * mockModuleSyncService.deleteSchemaSetIfExists('cm-handle-2')
and: 'module sync service is invoked for each cm handle'
- 1 * mockModuleSyncService.syncAndCreateOrUpgradeSchemaSetAndAnchor(_) >> { args -> assertYamgModelCmHandleArgument(args, 'cm-handle-1') }
- 1 * mockModuleSyncService.syncAndCreateOrUpgradeSchemaSetAndAnchor(_) >> { args -> assertYamgModelCmHandleArgument(args, 'cm-handle-2') }
+ 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(_) >> { args -> assert args[0].id == 'cm-handle-1' }
+ 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(_) >> { args -> assert args[0].id == 'cm-handle-2' }
and: 'the state handler is called for the both cm handles'
1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(_) >> { args ->
assertBatch(args, ['cm-handle-1', 'cm-handle-2'], CmHandleState.READY)
def 'Module Sync ADVISED cm handle with failure during sync.'() {
given: 'a cm handle in an ADVISED state'
- def cmHandle = advisedCmHandleAsDataNode('cm-handle')
+ def cmHandle = cmHandleAsDataNodeByIdAndState('cm-handle', CmHandleState.ADVISED)
and: 'the inventory persistence cm handle returns a ADVISED state for the cm handle'
def cmHandleState = new CompositeState(cmHandleState: CmHandleState.ADVISED)
1 * mockInventoryPersistence.getCmHandleState('cm-handle') >> cmHandleState
and: 'module sync service attempts to sync the cm handle and throws an exception'
- 1 * mockModuleSyncService.syncAndCreateOrUpgradeSchemaSetAndAnchor(*_) >> { throw new Exception('some exception') }
+ 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(*_) >> { throw new Exception('some exception') }
when: 'module sync is executed'
objectUnderTest.performModuleSync([cmHandle], batchCount)
then: 'update lock reason, details and attempts is invoked'
- 1 * mockSyncUtils.updateLockReasonDetailsAndAttempts(cmHandleState, LockReasonCategory.MODULE_SYNC_FAILED, 'some exception')
+ 1 * mockSyncUtils.updateLockReasonDetailsAndAttempts(cmHandleState, MODULE_SYNC_FAILED, 'some exception')
and: 'the state handler is called to update the state to LOCKED'
1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(_) >> { args ->
assertBatch(args, ['cm-handle'], CmHandleState.LOCKED)
assert batchCount.get() == 4
}
+ def 'Failed cm handle during #scenario.'() {
+ given: 'a cm handle in LOCKED state'
+ def cmHandle = cmHandleAsDataNodeByIdAndState('cm-handle', CmHandleState.LOCKED)
+ and: 'the inventory persistence cm handle returns a LOCKED state with reason for the cm handle'
+ def expectedCmHandleState = new CompositeState(cmHandleState: cmHandleState, lockReason: CompositeState
+ .LockReason.builder().lockReasonCategory(lockReasonCategory).details(lockReasonDetails).build())
+ 1 * mockInventoryPersistence.getCmHandleState('cm-handle') >> expectedCmHandleState
+ and: 'module sync service attempts to sync/upgrade the cm handle and throws an exception'
+ mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(*_) >> { throw new Exception('some exception') }
+ mockModuleSyncService.syncAndUpgradeSchemaSet(*_) >> { throw new Exception('some exception') }
+ when: 'module sync is executed'
+ objectUnderTest.performModuleSync([cmHandle], batchCount)
+ then: 'update lock reason, details and attempts is invoked'
+ 1 * mockSyncUtils.updateLockReasonDetailsAndAttempts(expectedCmHandleState, expectedLockReasonCategory, 'some exception')
+ where:
+ scenario | cmHandleState | lockReasonCategory | lockReasonDetails || expectedLockReasonCategory
+ 'module upgrade' | CmHandleState.LOCKED | MODULE_UPGRADE_FAILED | 'Upgrade to ModuleSetTag: some-module-set-tag' || MODULE_UPGRADE_FAILED
+ 'module sync' | CmHandleState.LOCKED | MODULE_SYNC_FAILED | 'some lock details' || MODULE_SYNC_FAILED
+ }
+
def 'Reset failed CM Handles #scenario.'() {
given: 'cm handles in an locked state'
def lockedState = new CompositeStateBuilder().withCmHandleState(CmHandleState.LOCKED)
def 'Module Sync ADVISED cm handle without entry in progress map.'() {
given: 'cm handles in an ADVISED state'
- def cmHandle1 = advisedCmHandleAsDataNode('cm-handle-1')
+ def cmHandle1 = cmHandleAsDataNodeByIdAndState('cm-handle-1', CmHandleState.ADVISED)
and: 'the inventory persistence cm handle returns a ADVISED state for the any handle'
mockInventoryPersistence.getCmHandleState(_) >> new CompositeState(cmHandleState: CmHandleState.ADVISED)
and: 'entry in progress map for other cm handle'
when: 'module sync poll is executed'
objectUnderTest.performModuleSync([cmHandle1], batchCount)
then: 'module sync service is invoked for cm handle'
- 1 * mockModuleSyncService.syncAndCreateOrUpgradeSchemaSetAndAnchor(_) >> { args -> assertYamgModelCmHandleArgument(args, 'cm-handle-1') }
+ 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(_) >> { args -> assertYamgModelCmHandleArgument(args, 'cm-handle-1') }
and: 'the entry for other cm handle is still in the progress map'
assert moduleSyncStartedOnCmHandles.get('other-cm-handle') != null
}
assert loggingEvent == null
}
- def advisedCmHandleAsDataNode(cmHandleId) {
- return new DataNode(anchorName: cmHandleId, leaves: ['id': cmHandleId, 'cm-handle-state': 'ADVISED'])
- }
-
- def assertYamgModelCmHandleArgument(args, expectedCmHandleId) {
- {
- def yangModelCmHandle = args[0]
- assert yangModelCmHandle.id == expectedCmHandleId
- }
- return true
+ def cmHandleAsDataNodeByIdAndState(cmHandleId, cmHandleState) {
+ return new DataNode(anchorName: cmHandleId, leaves: ['id': cmHandleId, 'cm-handle-state': cmHandleState])
}
def assertBatch(args, expectedCmHandleStatePerCmHandleIds, expectedCmHandleState) {