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