2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 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.impl.inventory.sync
24 import ch.qos.logback.classic.Level
25 import ch.qos.logback.classic.Logger
26 import ch.qos.logback.classic.spi.ILoggingEvent
27 import ch.qos.logback.core.read.ListAppender
28 import com.hazelcast.config.Config
29 import com.hazelcast.instance.impl.HazelcastInstanceFactory
30 import com.hazelcast.map.IMap
31 import org.junit.jupiter.api.AfterEach
32 import org.junit.jupiter.api.BeforeEach
33 import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler
34 import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleSyncService
35 import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleSyncTasks
36 import org.onap.cps.ncmp.api.impl.inventory.sync.SyncUtils
37 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
38 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
39 import org.onap.cps.ncmp.api.impl.inventory.CompositeState
40 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder
41 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence
42 import org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory
43 import org.onap.cps.spi.model.DataNode
44 import org.slf4j.LoggerFactory
45 import spock.lang.Specification
46 import java.util.concurrent.atomic.AtomicInteger
48 class ModuleSyncTasksSpec extends Specification {
50 def logger = Spy(ListAppender<ILoggingEvent>)
54 ((Logger) LoggerFactory.getLogger(ModuleSyncTasks.class)).addAppender(logger);
60 ((Logger) LoggerFactory.getLogger(ModuleSyncTasks.class)).detachAndStopAllAppenders();
63 def mockInventoryPersistence = Mock(InventoryPersistence)
65 def mockSyncUtils = Mock(SyncUtils)
67 def mockModuleSyncService = Mock(ModuleSyncService)
69 def mockLcmEventsCmHandleStateHandler = Mock(LcmEventsCmHandleStateHandler)
71 IMap<String, Object> moduleSyncStartedOnCmHandles = HazelcastInstanceFactory
72 .getOrCreateHazelcastInstance(new Config('hazelcastInstanceName'))
73 .getMap('mapInstanceName')
75 def batchCount = new AtomicInteger(5)
77 def objectUnderTest = new ModuleSyncTasks(mockInventoryPersistence, mockSyncUtils, mockModuleSyncService,
78 mockLcmEventsCmHandleStateHandler, moduleSyncStartedOnCmHandles)
80 def 'Module Sync ADVISED cm handles.'() {
81 given: 'cm handles in an ADVISED state'
82 def cmHandle1 = advisedCmHandleAsDataNode('cm-handle-1')
83 def cmHandle2 = advisedCmHandleAsDataNode('cm-handle-2')
84 and: 'the inventory persistence cm handle returns a ADVISED state for the any handle'
85 mockInventoryPersistence.getCmHandleState(_) >> new CompositeState(cmHandleState: CmHandleState.ADVISED)
86 when: 'module sync poll is executed'
87 objectUnderTest.performModuleSync([cmHandle1, cmHandle2], batchCount)
88 then: 'module sync service deletes schemas set of each cm handle if it already exists'
89 1 * mockModuleSyncService.deleteSchemaSetIfExists('cm-handle-1')
90 1 * mockModuleSyncService.deleteSchemaSetIfExists('cm-handle-2')
91 and: 'module sync service is invoked for each cm handle'
92 1 * mockModuleSyncService.syncAndCreateOrUpgradeSchemaSetAndAnchor(_) >> { args -> assertYamgModelCmHandleArgument(args, 'cm-handle-1') }
93 1 * mockModuleSyncService.syncAndCreateOrUpgradeSchemaSetAndAnchor(_) >> { args -> assertYamgModelCmHandleArgument(args, 'cm-handle-2') }
94 and: 'the state handler is called for the both cm handles'
95 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(_) >> { args ->
96 assertBatch(args, ['cm-handle-1', 'cm-handle-2'], CmHandleState.READY)
98 and: 'batch count is decremented by one'
99 assert batchCount.get() == 4
102 def 'Module Sync ADVISED cm handle with failure during sync.'() {
103 given: 'a cm handle in an ADVISED state'
104 def cmHandle = advisedCmHandleAsDataNode('cm-handle')
105 and: 'the inventory persistence cm handle returns a ADVISED state for the cm handle'
106 def cmHandleState = new CompositeState(cmHandleState: CmHandleState.ADVISED)
107 1 * mockInventoryPersistence.getCmHandleState('cm-handle') >> cmHandleState
108 and: 'module sync service attempts to sync the cm handle and throws an exception'
109 1 * mockModuleSyncService.syncAndCreateOrUpgradeSchemaSetAndAnchor(*_) >> { throw new Exception('some exception') }
110 when: 'module sync is executed'
111 objectUnderTest.performModuleSync([cmHandle], batchCount)
112 then: 'update lock reason, details and attempts is invoked'
113 1 * mockSyncUtils.updateLockReasonDetailsAndAttempts(cmHandleState, LockReasonCategory.MODULE_SYNC_FAILED, 'some exception')
114 and: 'the state handler is called to update the state to LOCKED'
115 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(_) >> { args ->
116 assertBatch(args, ['cm-handle'], CmHandleState.LOCKED)
118 and: 'batch count is decremented by one'
119 assert batchCount.get() == 4
122 def 'Reset failed CM Handles #scenario.'() {
123 given: 'cm handles in an locked state'
124 def lockedState = new CompositeStateBuilder().withCmHandleState(CmHandleState.LOCKED)
125 .withLockReason(LockReasonCategory.MODULE_SYNC_FAILED, '').withLastUpdatedTimeNow().build()
126 def yangModelCmHandle1 = new YangModelCmHandle(id: 'cm-handle-1', compositeState: lockedState)
127 def yangModelCmHandle2 = new YangModelCmHandle(id: 'cm-handle-2', compositeState: lockedState)
128 def expectedCmHandleStatePerCmHandle = [(yangModelCmHandle1): CmHandleState.ADVISED]
129 and: 'clear in progress map'
130 resetModuleSyncStartedOnCmHandles(moduleSyncStartedOnCmHandles)
131 and: 'add cm handle entry into progress map'
132 moduleSyncStartedOnCmHandles.put('cm-handle-1', 'started')
133 moduleSyncStartedOnCmHandles.put('cm-handle-2', 'started')
134 and: 'sync utils retry locked cm handle returns #isReadyForRetry'
135 mockSyncUtils.needsModuleSyncRetryOrUpgrade(lockedState) >>> isReadyForRetry
136 when: 'resetting failed cm handles'
137 objectUnderTest.resetFailedCmHandles([yangModelCmHandle1, yangModelCmHandle2])
138 then: 'updated to state "ADVISED" from "READY" is called as often as there are cm handles ready for retry'
139 expectedNumberOfInvocationsToUpdateCmHandleState * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(expectedCmHandleStatePerCmHandle)
140 and: 'after reset performed size of in progress map'
141 assert moduleSyncStartedOnCmHandles.size() == inProgressMapSize
143 scenario | isReadyForRetry | inProgressMapSize || expectedNumberOfInvocationsToUpdateCmHandleState
144 'retry locked cm handle' | [true, false] | 1 || 1
145 'do not retry locked cm handle' | [false, false] | 2 || 0
148 def 'Module Sync ADVISED cm handle without entry in progress map.'() {
149 given: 'cm handles in an ADVISED state'
150 def cmHandle1 = advisedCmHandleAsDataNode('cm-handle-1')
151 and: 'the inventory persistence cm handle returns a ADVISED state for the any handle'
152 mockInventoryPersistence.getCmHandleState(_) >> new CompositeState(cmHandleState: CmHandleState.ADVISED)
153 and: 'entry in progress map for other cm handle'
154 moduleSyncStartedOnCmHandles.put('other-cm-handle', 'started')
155 when: 'module sync poll is executed'
156 objectUnderTest.performModuleSync([cmHandle1], batchCount)
157 then: 'module sync service is invoked for cm handle'
158 1 * mockModuleSyncService.syncAndCreateOrUpgradeSchemaSetAndAnchor(_) >> { args -> assertYamgModelCmHandleArgument(args, 'cm-handle-1') }
159 and: 'the entry for other cm handle is still in the progress map'
160 assert moduleSyncStartedOnCmHandles.get('other-cm-handle') != null
163 def 'Remove already processed cm handle id from hazelcast map'() {
164 given: 'hazelcast map contains cm handle id'
165 moduleSyncStartedOnCmHandles.put('ch-1', 'started')
166 when: 'remove cm handle entry'
167 objectUnderTest.removeResetCmHandleFromModuleSyncMap('ch-1')
168 then: 'an event is logged with level INFO'
169 def loggingEvent = getLoggingEvent()
170 assert loggingEvent.level == Level.INFO
171 and: 'the log indicates the cm handle entry is removed successfully'
172 assert loggingEvent.formattedMessage == 'ch-1 removed from in progress map'
175 def 'Remove non-existing cm handle id from hazelcast map'() {
176 given: 'hazelcast map does not contains cm handle id'
177 def result = moduleSyncStartedOnCmHandles.get('non-existing-cm-handle')
178 assert result == null
179 when: 'remove cm handle entry from hazelcast map'
180 objectUnderTest.removeResetCmHandleFromModuleSyncMap('non-existing-cm-handle')
181 then: 'no event is logged'
182 def loggingEvent = getLoggingEvent()
183 assert loggingEvent == null
186 def advisedCmHandleAsDataNode(cmHandleId) {
187 return new DataNode(anchorName: cmHandleId, leaves: ['id': cmHandleId, 'cm-handle-state': 'ADVISED'])
190 def assertYamgModelCmHandleArgument(args, expectedCmHandleId) {
192 def yangModelCmHandle = args[0]
193 assert yangModelCmHandle.id == expectedCmHandleId
198 def assertBatch(args, expectedCmHandleStatePerCmHandleIds, expectedCmHandleState) {
200 Map<YangModelCmHandle, CmHandleState> actualCmHandleStatePerCmHandle = args[0]
201 assert actualCmHandleStatePerCmHandle.size() == expectedCmHandleStatePerCmHandleIds.size()
202 actualCmHandleStatePerCmHandle.each {
203 assert expectedCmHandleStatePerCmHandleIds.contains(it.key.id)
204 assert it.value == expectedCmHandleState
210 def resetModuleSyncStartedOnCmHandles(moduleSyncStartedOnCmHandles) {
211 moduleSyncStartedOnCmHandles.clear();
214 def getLoggingEvent() {
215 return logger.list[0]