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