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.mockito.Mock
25 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler
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.DataStoreSyncState
30 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
31 import org.onap.cps.ncmp.api.inventory.LockReasonCategory
32 import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder
33 import spock.lang.Specification
35 import java.util.concurrent.ConcurrentHashMap
36 import java.util.concurrent.ConcurrentMap
38 class ModuleSyncWatchdogSpec extends Specification {
40 def mockInventoryPersistence = Mock(InventoryPersistence)
42 def mockSyncUtils = Mock(SyncUtils)
44 def mockModuleSyncService = Mock(ModuleSyncService)
46 def stubbedMap = Stub(ConcurrentMap)
48 def mockLcmEventsCmHandleStateHandler = Mock(LcmEventsCmHandleStateHandler)
50 def cmHandleState = CmHandleState.ADVISED
52 def objectUnderTest = new ModuleSyncWatchdog(mockInventoryPersistence, mockSyncUtils, mockModuleSyncService, stubbedMap as ConcurrentHashMap, mockLcmEventsCmHandleStateHandler)
54 def 'Schedule a Cm-Handle Sync for ADVISED Cm-Handles'() {
55 given: 'cm handles in an advised state and a data sync state'
56 def compositeState1 = new CompositeState(cmHandleState: cmHandleState)
57 def compositeState2 = new CompositeState(cmHandleState: cmHandleState)
58 def yangModelCmHandle1 = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState1)
59 def yangModelCmHandle2 = new YangModelCmHandle(id: 'some-cm-handle-2', compositeState: compositeState2)
60 and: 'sync utilities return a cm handle twice'
61 mockSyncUtils.getAdvisedCmHandles() >> [yangModelCmHandle1, yangModelCmHandle2]
62 when: 'module sync poll is executed'
63 objectUnderTest.executeAdvisedCmHandlePoll()
64 then: 'the inventory persistence cm handle returns a composite state for the first cm handle'
65 1 * mockInventoryPersistence.getCmHandleState('some-cm-handle') >> compositeState1
66 and: 'module sync service deletes schema set of cm handle if it exists'
67 1 * mockModuleSyncService.deleteSchemaSetIfExists(yangModelCmHandle1)
68 and: 'module sync service syncs the first cm handle and creates a schema set'
69 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle1)
70 then: 'the state handler is called for the first cm handle'
71 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle1, CmHandleState.READY)
72 and: 'the inventory persistence cm handle returns a composite state for the second cm handle'
73 mockInventoryPersistence.getCmHandleState('some-cm-handle-2') >> compositeState2
74 and: 'module sync service syncs the second cm handle and creates a schema set'
75 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle2)
76 then: 'the state handler is called for the second cm handle'
77 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle2, CmHandleState.READY)
80 def 'Schedule a Cm-Handle Sync for ADVISED Cm-Handle with failure'() {
81 given: 'cm handles in an advised state'
82 def compositeState = new CompositeState(cmHandleState: cmHandleState)
83 def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState)
84 and: 'sync utilities return a cm handle'
85 mockSyncUtils.getAdvisedCmHandles() >> [yangModelCmHandle]
86 when: 'module sync poll is executed'
87 objectUnderTest.executeAdvisedCmHandlePoll()
88 then: 'the inventory persistence cm handle returns a composite state for the cm handle'
89 1 * mockInventoryPersistence.getCmHandleState('some-cm-handle') >> compositeState
90 and: 'module sync service attempts to sync the cm handle and throws an exception'
91 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(*_) >> { throw new Exception('some exception') }
92 and: 'update lock reason, details and attempts is invoked'
93 1 * mockSyncUtils.updateLockReasonDetailsAndAttempts(compositeState, LockReasonCategory.LOCKED_MODULE_SYNC_FAILED ,'some exception')
94 and: 'the state handler is called to update the state to LOCKED'
95 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle, CmHandleState.LOCKED)
98 def 'Schedule a Cm-Handle Sync with condition #scenario '() {
99 given: 'cm handles in an locked state'
100 def compositeState = new CompositeStateBuilder().withCmHandleState(CmHandleState.LOCKED)
101 .withLockReason(LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, '').withLastUpdatedTimeNow().build()
102 def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState)
103 and: 'sync utilities return a cm handle twice'
104 mockSyncUtils.getModuleSyncFailedCmHandles() >> [yangModelCmHandle, yangModelCmHandle]
105 and: 'inventory persistence returns the composite state of the cm handle'
106 mockInventoryPersistence.getCmHandleState(yangModelCmHandle.getId()) >> compositeState
107 and: 'sync utils retry locked cm handle returns #isReadyForRetry'
108 mockSyncUtils.isReadyForRetry(compositeState) >>> isReadyForRetry
109 when: 'module sync poll is executed'
110 objectUnderTest.executeLockedCmHandlePoll()
111 then: 'the first cm handle is updated to state "ADVISED" from "READY"'
112 expectedNumberOfInvocationsToSaveCmHandleState * mockLcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle, CmHandleState.ADVISED)
114 scenario | isReadyForRetry || expectedNumberOfInvocationsToSaveCmHandleState
115 'retry locked cm handle once' | [true, false] || 1
116 'retry locked cm handle twice' | [true, true] || 2
117 'do not retry locked cm handle' | [false, false] || 0