68aa6a1b6a4836aa3279cd5f4130af02a209bef2
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2025 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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.impl.inventory.sync
23
24 import com.hazelcast.map.IMap
25 import java.util.concurrent.ArrayBlockingQueue
26 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle
27 import spock.lang.Specification
28
29 class ModuleSyncWatchdogSpec extends Specification {
30
31     def mockModuleOperationsUtils = Mock(ModuleOperationsUtils)
32
33     def static testQueueCapacity = 50 + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE
34
35     def moduleSyncWorkQueue = new ArrayBlockingQueue(testQueueCapacity)
36
37     def mockModuleSyncStartedOnCmHandles = Mock(IMap<String, Object>)
38
39     def mockModuleSyncTasks = Mock(ModuleSyncTasks)
40
41     def mockCpsAndNcmpLock = Mock(IMap<String,String>)
42
43     def objectUnderTest = new ModuleSyncWatchdog(mockModuleOperationsUtils, moduleSyncWorkQueue , mockModuleSyncStartedOnCmHandles, mockModuleSyncTasks, mockCpsAndNcmpLock)
44
45     def 'Module sync advised cm handles with #scenario.'() {
46         given: 'module sync utilities returns #numberOfAdvisedCmHandles advised cm handles'
47             mockModuleOperationsUtils.getAdvisedCmHandleIds() >> createCmHandleIds(numberOfAdvisedCmHandles)
48         and: 'module sync utilities returns no failed (locked) cm handles'
49             mockModuleOperationsUtils.getCmHandlesThatFailedModelSyncOrUpgrade() >> []
50         and: 'the work queue can be locked'
51             mockCpsAndNcmpLock.tryLock('workQueueLock') >> true
52         when: ' module sync is started'
53             objectUnderTest.moduleSyncAdvisedCmHandles()
54         then: 'it performs #expectedNumberOfTaskExecutions tasks'
55             expectedNumberOfTaskExecutions * mockModuleSyncTasks.performModuleSync(*_)
56         and: 'the executing thread is unlocked'
57             1 * mockCpsAndNcmpLock.unlock('workQueueLock')
58         where: 'the following parameter are used'
59             scenario              | numberOfAdvisedCmHandles                                          || expectedNumberOfTaskExecutions
60             'none at all'         | 0                                                                 || 0
61             'less then 1 batch'   | 1                                                                 || 1
62             'exactly 1 batch'     | ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE                         || 1
63             '2 batches'           | 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE                     || 2
64             'queue capacity'      | testQueueCapacity                                                 || 3
65             'over queue capacity' | testQueueCapacity + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 3
66     }
67
68     def 'Module sync cm handles starts with no available threads.'() {
69         given: 'module sync utilities returns a advise cm handles'
70             mockModuleOperationsUtils.getAdvisedCmHandleIds() >> createCmHandleIds(1)
71         and: 'the work queue can be locked'
72             mockCpsAndNcmpLock.tryLock('workQueueLock') >> true
73         when: ' module sync is started'
74             objectUnderTest.moduleSyncAdvisedCmHandles()
75         then: 'it performs one task'
76             1 * mockModuleSyncTasks.performModuleSync(*_)
77     }
78
79     def 'Module sync advised cm handle already handled by other thread.'() {
80         given: 'module sync utilities returns an advised cm handle'
81             mockModuleOperationsUtils.getAdvisedCmHandleIds() >> createCmHandleIds(1)
82         and: 'the work queue can be locked'
83             mockCpsAndNcmpLock.tryLock('workQueueLock') >> true
84         and: 'the semaphore cache indicates the cm handle is already being processed'
85             mockModuleSyncStartedOnCmHandles.putIfAbsent(*_) >> 'Started'
86         when: 'module sync is started'
87             objectUnderTest.moduleSyncAdvisedCmHandles()
88         then: 'it does NOT execute a task to process the (empty) batch'
89             0 * mockModuleSyncTasks.performModuleSync(*_)
90     }
91
92     def 'Module sync with previous cm handle(s) left in work queue.'() {
93         given: 'there is still a cm handle in the queue'
94             moduleSyncWorkQueue.offer('ch-1')
95         when: 'module sync is started'
96             objectUnderTest.moduleSyncAdvisedCmHandles()
97         then: 'it does executes only one task to process the remaining handle in the queue'
98             1 * mockModuleSyncTasks.performModuleSync(*_)
99     }
100
101     def 'Reset failed cm handles.'() {
102         given: 'module sync utilities returns failed cm handles'
103             def failedCmHandles = [new YangModelCmHandle()]
104             mockModuleOperationsUtils.getCmHandlesThatFailedModelSyncOrUpgrade() >> failedCmHandles
105         when: 'reset failed cm handles is started'
106             objectUnderTest.setPreviouslyLockedCmHandlesToAdvised()
107         then: 'it is delegated to the module sync task (service)'
108             1 * mockModuleSyncTasks.setCmHandlesToAdvised(failedCmHandles)
109     }
110
111     def 'Module Sync Locking.'() {
112         given: 'module sync utilities returns an advised cm handle'
113             mockModuleOperationsUtils.getAdvisedCmHandleIds() >> createCmHandleIds(1)
114         and: 'can be locked is : #canLock'
115             mockCpsAndNcmpLock.tryLock('workQueueLock') >> canLock
116         when: 'attempt to populate the work queue'
117             objectUnderTest.populateWorkQueueIfNeeded()
118         then: 'the queue remains empty is #expectQueueRemainsEmpty'
119             assert moduleSyncWorkQueue.isEmpty() == expectQueueRemainsEmpty
120         and: 'unlock is called only when thread is able to enter the critical section'
121             expectedInvocationToUnlock * mockCpsAndNcmpLock.unlock('workQueueLock')
122         where: 'the following lock states are applied'
123             canLock || expectQueueRemainsEmpty || expectedInvocationToUnlock
124             false   || true                    || 0
125             true    || false                   || 1
126     }
127
128     def createCmHandleIds(numberOfCmHandles) {
129         return (numberOfCmHandles > 0) ? (1..numberOfCmHandles).collect { 'ch-'+it } : []
130     }
131 }