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 where #scenario'() {
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 objectUnderTest.isGlobalDataSyncCacheEnabled = dataSyncCacheEnabled
57 and: 'sync utilities return a cm handle twice'
58 mockSyncUtils.getAdvisedCmHandles() >> [yangModelCmHandle1, yangModelCmHandle2]
59 when: 'module sync poll is executed'
60 objectUnderTest.executeAdvisedCmHandlePoll()
61 then: 'the inventory persistence cm handle returns a composite state for the first cm handle'
62 1 * mockInventoryPersistence.getCmHandleState('some-cm-handle') >> compositeState1
63 and: 'module sync service deletes schema set of cm handle if it exists'
64 1 * mockModuleSyncService.deleteSchemaSetIfExists(yangModelCmHandle1)
65 and: 'module sync service syncs the first cm handle and creates a schema set'
66 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle1)
67 then: 'the composite state cm handle state is now READY'
68 assert compositeState1.getCmHandleState() == CmHandleState.READY
69 and: 'the data store sync state returns the expected state'
70 compositeState1.getDataStores().operationalDataStore.dataStoreSyncState == expectedDataStoreSyncState
71 and: 'the first cm handle state is updated'
72 1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle', compositeState1)
73 then: 'the inventory persistence cm handle returns a composite state for the second cm handle'
74 mockInventoryPersistence.getCmHandleState('some-cm-handle-2') >> compositeState2
75 and: 'module sync service syncs the second cm handle and creates a schema set'
76 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(yangModelCmHandle2)
77 and: 'the composite state cm handle state is now READY'
78 assert compositeState2.getCmHandleState() == CmHandleState.READY
79 and: 'the second cm handle state is updated'
80 1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle-2', compositeState2)
82 scenario | dataSyncCacheEnabled || expectedDataStoreSyncState
83 'data sync cache enabled' | true || DataStoreSyncState.UNSYNCHRONIZED
84 'data sync cache is not enabled' | false || DataStoreSyncState.NONE_REQUESTED
87 def 'Schedule a Cm-Handle Sync for ADVISED Cm-Handle with failure'() {
88 given: 'cm handles in an advised state'
89 def compositeState = new CompositeState(cmHandleState: cmHandleState)
90 def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState)
91 and: 'sync utilities return a cm handle'
92 mockSyncUtils.getAdvisedCmHandles() >> [yangModelCmHandle]
93 when: 'module sync poll is executed'
94 objectUnderTest.executeAdvisedCmHandlePoll()
95 then: 'the inventory persistence cm handle returns a composite state for the cm handle'
96 1 * mockInventoryPersistence.getCmHandleState('some-cm-handle') >> compositeState
97 and: 'module sync service attempts to sync the cm handle and throws an exception'
98 1 * mockModuleSyncService.syncAndCreateSchemaSetAndAnchor(*_) >> { throw new Exception('some exception') }
99 and: 'the composite state cm handle state is now LOCKED'
100 assert compositeState.getCmHandleState() == CmHandleState.LOCKED
101 and: 'update lock reason, details and attempts is invoked'
102 1 * mockSyncUtils.updateLockReasonDetailsAndAttempts(compositeState, LockReasonCategory.LOCKED_MODULE_SYNC_FAILED ,'some exception')
103 and: 'the cm handle state is updated'
104 1 * mockInventoryPersistence.saveCmHandleState('some-cm-handle', compositeState)
108 def 'Schedule a Cm-Handle Sync with condition #scenario '() {
109 given: 'cm handles in an locked state'
110 def compositeState = new CompositeStateBuilder().withCmHandleState(CmHandleState.LOCKED)
111 .withLockReason(LockReasonCategory.LOCKED_MODULE_SYNC_FAILED, '').withLastUpdatedTimeNow().build()
112 def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', compositeState: compositeState)
113 and: 'sync utilities return a cm handle twice'
114 mockSyncUtils.getModuleSyncFailedCmHandles() >> [yangModelCmHandle, yangModelCmHandle]
115 and: 'inventory persistence returns the composite state of the cm handle'
116 mockInventoryPersistence.getCmHandleState(yangModelCmHandle.getId()) >> compositeState
117 and: 'sync utils retry locked cm handle returns #isReadyForRetry'
118 mockSyncUtils.isReadyForRetry(compositeState) >>> isReadyForRetry
119 when: 'module sync poll is executed'
120 objectUnderTest.executeLockedCmHandlePoll()
121 then: 'the first cm handle is updated to state "ADVISED" from "READY"'
122 expectedNumberOfInvocationsToSaveCmHandleState * mockInventoryPersistence.saveCmHandleState(yangModelCmHandle.id, compositeState)
124 scenario | isReadyForRetry || expectedNumberOfInvocationsToSaveCmHandleState
125 'retry locked cm handle once' | [true, false] || 1
126 'retry locked cm handle twice' | [true, true] || 2
127 'do not retry locked cm handle' | [false, false] || 0