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.yangmodels.YangModelCmHandle
26 import org.onap.cps.ncmp.api.inventory.CmHandleState
27 import org.onap.cps.ncmp.api.inventory.CompositeState
28 import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder
29 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
30 import org.onap.cps.ncmp.api.inventory.LockReasonCategory
31 import org.onap.cps.spi.model.DataNode
32 import spock.lang.Specification
33 import java.util.concurrent.atomic.AtomicInteger
35 class ModuleSyncTasksSpec extends Specification {
37 def mockInventoryPersistence = Mock(InventoryPersistence)
39 def mockSyncUtils = Mock(SyncUtils)
41 def mockModuleSyncService = Mock(ModuleSyncService)
43 def mockLcmEventsCmHandleStateHandler = Mock(LcmEventsCmHandleStateHandler)
45 def batchCount = new AtomicInteger(5)
47 def objectUnderTest = new ModuleSyncTasks(mockInventoryPersistence, mockSyncUtils, mockModuleSyncService, mockLcmEventsCmHandleStateHandler)
49 def 'Module Sync ADVISED cm handles.'() {
50 given: 'cm handles in an ADVISED state'
51 def cmHandle1 = advisedCmHandleAsDataNode('cm-handle-1')
52 def cmHandle2 = advisedCmHandleAsDataNode('cm-handle-2')
53 and: 'the inventory persistence cm handle returns a ADVISED state for the any handle'
54 mockInventoryPersistence.getCmHandleState(_) >> new CompositeState(cmHandleState: CmHandleState.ADVISED)
55 when: 'module sync poll is executed'
56 objectUnderTest.performModuleSync([cmHandle1, cmHandle2], batchCount)
57 then: 'module sync service deletes schemas set of each cm handle if it already exists'
58 1 * mockModuleSyncService.deleteSchemaSetIfExists('cm-handle-1')
59 1 * mockModuleSyncService.deleteSchemaSetIfExists('cm-handle-2')
60 and: 'module sync service is invoked for each cm handle'
61 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(_) >> { args -> assertYamgModelCmHandleArgument(args, 'cm-handle-1') }
62 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(_) >> { args -> assertYamgModelCmHandleArgument(args, 'cm-handle-2') }
63 and: 'the state handler is called for the both cm handles'
64 2 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.READY)
65 and: 'batch count is decremented by one'
66 assert batchCount.get() == 4
69 def 'Module Sync ADVISED cm handle with failure during sync.'() {
70 given: 'a cm handle in an ADVISED state'
71 def cmHandle = advisedCmHandleAsDataNode('cm-handle')
72 and: 'the inventory persistence cm handle returns a ADVISED state for the cm handle'
73 def cmHandleState = new CompositeState(cmHandleState: CmHandleState.ADVISED)
74 1 * mockInventoryPersistence.getCmHandleState('cm-handle') >> cmHandleState
75 and: 'module sync service attempts to sync the cm handle and throws an exception'
76 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(*_) >> { throw new Exception('some exception') }
77 when: 'module sync is executed'
78 objectUnderTest.performModuleSync([cmHandle], batchCount)
79 then: 'update lock reason, details and attempts is invoked'
80 1 * mockSyncUtils.updateLockReasonDetailsAndAttempts(cmHandleState, LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, 'some exception')
81 and: 'the state handler is called to update the state to LOCKED'
82 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.LOCKED)
83 and: 'batch count is decremented by one'
84 assert batchCount.get() == 4
87 def 'Reset failed CM Handles #scenario.'() {
88 given: 'cm handles in an locked state'
89 def lockedState = new CompositeStateBuilder().withCmHandleState(CmHandleState.LOCKED)
90 .withLockReason(LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, '').withLastUpdatedTimeNow().build()
91 def yangModelCmHandle1 = new YangModelCmHandle(id: 'cm-handle-1', compositeState: lockedState)
92 def yangModelCmHandle2 = new YangModelCmHandle(id: 'cm-handle-2', compositeState: lockedState)
93 and: 'sync utils retry locked cm handle returns #isReadyForRetry'
94 mockSyncUtils.isReadyForRetry(lockedState) >>> isReadyForRetry
95 when: 'resetting failed cm handles'
96 objectUnderTest.resetFailedCmHandles([yangModelCmHandle1, yangModelCmHandle2])
97 then: 'updated to state "ADVISED" from "READY" is called as often as there are cm handles ready for retry'
98 expectedNumberOfInvocationsToSaveCmHandleState * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.ADVISED)
100 scenario | isReadyForRetry || expectedNumberOfInvocationsToSaveCmHandleState
101 'retry locked cm handle once' | [true, false] || 1
102 'retry locked cm handle twice' | [true, true] || 2
103 'do not retry locked cm handle' | [false, false] || 0
106 def advisedCmHandleAsDataNode(cmHandleId) {
107 return new DataNode(anchorName: cmHandleId, leaves: ['id': cmHandleId, 'cm-handle-state': 'ADVISED'])
110 def assertYamgModelCmHandleArgument(args, expectedCmHandleId) {
112 def yangModelCmHandle = args[0]
113 assert yangModelCmHandle.id == expectedCmHandleId