2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation
4 * Modifications Copyright (C) 2022 Bell Canada
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.inventory.sync
24 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler
25 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter
26 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
27 import org.onap.cps.ncmp.api.inventory.CmHandleState
28 import org.onap.cps.ncmp.api.inventory.CompositeState
29 import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder
30 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
31 import org.onap.cps.ncmp.api.inventory.LockReasonCategory
32 import org.onap.cps.spi.model.DataNode
33 import spock.lang.Specification
34 import java.util.concurrent.atomic.AtomicInteger
36 class ModuleSyncTasksSpec extends Specification {
38 def mockInventoryPersistence = Mock(InventoryPersistence)
40 def mockSyncUtils = Mock(SyncUtils)
42 def mockModuleSyncService = Mock(ModuleSyncService)
44 def mockLcmEventsCmHandleStateHandler = Mock(LcmEventsCmHandleStateHandler)
46 def batchCount = new AtomicInteger(5)
48 def objectUnderTest = new ModuleSyncTasks(mockInventoryPersistence, mockSyncUtils, mockModuleSyncService, mockLcmEventsCmHandleStateHandler)
50 def 'Module Sync ADVISED cm handles.'() {
51 given: 'cm handles in an ADVISED state'
52 def cmHandle1 = advisedCmHandleAsDataNode('cm-handle-1')
53 def cmHandle2 = advisedCmHandleAsDataNode('cm-handle-2')
54 and: 'the inventory persistence cm handle returns a ADVISED state for the any handle'
55 mockInventoryPersistence.getCmHandleState(_) >> new CompositeState(cmHandleState: CmHandleState.ADVISED)
56 when: 'module sync poll is executed'
57 objectUnderTest.performModuleSync([cmHandle1, cmHandle2], batchCount)
58 then: 'module sync service deletes schemas set of each cm handle if it already exists'
59 1 * mockModuleSyncService.deleteSchemaSetIfExists('cm-handle-1')
60 1 * mockModuleSyncService.deleteSchemaSetIfExists('cm-handle-2')
61 and: 'module sync service is invoked for each cm handle'
62 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(_) >> { args -> assertYamgModelCmHandleArgument(args, 'cm-handle-1') }
63 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(_) >> { args -> assertYamgModelCmHandleArgument(args, 'cm-handle-2') }
64 and: 'the state handler is called for the both cm handles'
65 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(_) >> { args ->
66 assertBatch(args, ['cm-handle-1', 'cm-handle-2'], CmHandleState.READY)
68 and: 'batch count is decremented by one'
69 assert batchCount.get() == 4
72 def 'Module Sync ADVISED cm handle with failure during sync.'() {
73 given: 'a cm handle in an ADVISED state'
74 def cmHandle = advisedCmHandleAsDataNode('cm-handle')
75 and: 'the inventory persistence cm handle returns a ADVISED state for the cm handle'
76 def cmHandleState = new CompositeState(cmHandleState: CmHandleState.ADVISED)
77 1 * mockInventoryPersistence.getCmHandleState('cm-handle') >> cmHandleState
78 and: 'module sync service attempts to sync the cm handle and throws an exception'
79 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(*_) >> { throw new Exception('some exception') }
80 when: 'module sync is executed'
81 objectUnderTest.performModuleSync([cmHandle], batchCount)
82 then: 'update lock reason, details and attempts is invoked'
83 1 * mockSyncUtils.updateLockReasonDetailsAndAttempts(cmHandleState, LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, 'some exception')
84 and: 'the state handler is called to update the state to LOCKED'
85 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(_) >> { args ->
86 assertBatch(args, ['cm-handle'], CmHandleState.LOCKED)
88 and: 'batch count is decremented by one'
89 assert batchCount.get() == 4
92 def 'Reset failed CM Handles #scenario.'() {
93 given: 'cm handles in an locked state'
94 def lockedState = new CompositeStateBuilder().withCmHandleState(CmHandleState.LOCKED)
95 .withLockReason(LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, '').withLastUpdatedTimeNow().build()
96 def yangModelCmHandle1 = new YangModelCmHandle(id: 'cm-handle-1', compositeState: lockedState)
97 def yangModelCmHandle2 = new YangModelCmHandle(id: 'cm-handle-2', compositeState: lockedState)
98 and: 'sync utils retry locked cm handle returns #isReadyForRetry'
99 mockSyncUtils.isReadyForRetry(lockedState) >>> isReadyForRetry
100 when: 'resetting failed cm handles'
101 objectUnderTest.resetFailedCmHandles([yangModelCmHandle1, yangModelCmHandle2])
102 then: 'updated to state "ADVISED" from "READY" is called as often as there are cm handles ready for retry'
103 // expectedNumberOfInvocationsToSaveCmHandleState * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.ADVISED)
105 scenario | isReadyForRetry || expectedNumberOfInvocationsToSaveCmHandleState
106 'retry locked cm handle once' | [true, false] || 1
107 'retry locked cm handle twice' | [true, true] || 2
108 'do not retry locked cm handle' | [false, false] || 0
111 def advisedCmHandleAsDataNode(cmHandleId) {
112 return new DataNode(anchorName: cmHandleId, leaves: ['id': cmHandleId, 'cm-handle-state': 'ADVISED'])
115 def assertYamgModelCmHandleArgument(args, expectedCmHandleId) {
117 def yangModelCmHandle = args[0]
118 assert yangModelCmHandle.id == expectedCmHandleId
123 def assertBatch(args, expectedCmHandleStatePerCmHandleIds, expectedCmHandleState) {
125 Map<YangModelCmHandle, CmHandleState> actualCmHandleStatePerCmHandle = args[0]
126 assert actualCmHandleStatePerCmHandle.size() == expectedCmHandleStatePerCmHandleIds.size()
127 actualCmHandleStatePerCmHandle.each {
128 assert expectedCmHandleStatePerCmHandleIds.contains(it.key.id)
129 assert it.value == expectedCmHandleState