28714fd1235d77743bf88ffc7be844e2786c4d20
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024-2025 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the 'License');
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an 'AS IS' BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.integration.functional.ncmp
22
23 import org.onap.cps.integration.base.CpsIntegrationSpecBase
24 import org.onap.cps.ncmp.api.inventory.models.CmHandleRegistrationResponse
25 import org.onap.cps.ncmp.api.inventory.models.CmHandleState
26 import org.onap.cps.ncmp.api.inventory.models.DmiPluginRegistration
27 import org.onap.cps.ncmp.api.inventory.models.LockReasonCategory
28 import org.onap.cps.ncmp.api.inventory.models.UpgradedCmHandles
29 import org.onap.cps.ncmp.impl.NetworkCmProxyInventoryFacadeImpl
30 import spock.util.concurrent.PollingConditions
31
32 class CmHandleUpgradeSpec extends CpsIntegrationSpecBase {
33
34     NetworkCmProxyInventoryFacadeImpl objectUnderTest
35
36     def cmHandleId = 'ch-1'
37     def cmHandleIdWithExistingModuleSetTag = 'ch-2'
38
39     def setup() {
40         objectUnderTest = networkCmProxyInventoryFacade
41     }
42
43     def 'Upgrade CM-handle with and without (new) module set tags.'() {
44         given: 'a CM-handle is created with expected initial modules: M1 and M2'
45             dmiDispatcher1.moduleNamesPerCmHandleId[cmHandleId] = ['M1', 'M2']
46             registerCmHandle(DMI1_URL, cmHandleId, initialModuleSetTag)
47             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(cmHandleId).moduleName.sort()
48
49         when: "the CM-handle is upgraded with given moduleSetTag '${updatedModuleSetTag}'"
50             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [cmHandleId], moduleSetTag: updatedModuleSetTag)
51             def dmiPluginRegistrationResponse = objectUnderTest.updateDmiRegistration(
52                     new DmiPluginRegistration(dmiPlugin: DMI1_URL, upgradedCmHandles: cmHandlesToUpgrade))
53
54         then: 'registration gives successful response'
55             assert dmiPluginRegistrationResponse.upgradedCmHandles == [CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)]
56
57         and: 'CM-handle is in LOCKED state due to MODULE_UPGRADE'
58             def cmHandleCompositeState = objectUnderTest.getCmHandleCompositeState(cmHandleId)
59             assert cmHandleCompositeState.cmHandleState == CmHandleState.LOCKED
60             assert cmHandleCompositeState.lockReason.lockReasonCategory == LockReasonCategory.MODULE_UPGRADE
61             assert cmHandleCompositeState.lockReason.details == "Upgrade to ModuleSetTag: ${updatedModuleSetTag}"
62
63         when: 'DMI will return different modules for upgrade: M1 and M3'
64             dmiDispatcher1.moduleNamesPerCmHandleId[cmHandleId] = ['M1', 'M3']
65
66         and: 'the module sync watchdog is triggered twice'
67             2.times { moduleSyncWatchdog.moduleSyncAdvisedCmHandles() }
68
69         then: 'CM-handle goes to READY state'
70             new PollingConditions().within(MODULE_SYNC_WAIT_TIME_IN_SECONDS, () -> {
71                 assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(cmHandleId).cmHandleState
72             })
73
74         and: 'the CM-handle has expected moduleSetTag'
75             assert objectUnderTest.getNcmpServiceCmHandle(cmHandleId).moduleSetTag == updatedModuleSetTag
76
77         and: 'CM-handle has expected updated modules: M1 and M3'
78             assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(cmHandleId).moduleName.sort()
79
80         cleanup: 'deregister CM-handle and remove all associated module resources'
81             deregisterCmHandle(DMI1_URL, cmHandleId)
82             cpsModuleService.deleteAllUnusedYangModuleData()
83
84         where: 'following module set tags are used'
85             initialModuleSetTag | updatedModuleSetTag
86             NO_MODULE_SET_TAG   | NO_MODULE_SET_TAG
87             NO_MODULE_SET_TAG   | 'new@set'
88             'initial set'       | NO_MODULE_SET_TAG
89             'initial set'       | 'new@set'
90     }
91
92     def 'Upgrade CM-handle with existing moduleSetTag.'() {
93         given: 'DMI will return modules for registration'
94             dmiDispatcher1.moduleNamesPerCmHandleId[cmHandleId] = ['M1', 'M2']
95             dmiDispatcher1.moduleNamesPerCmHandleId[cmHandleIdWithExistingModuleSetTag] = ['M1', 'M3']
96         and: "an existing CM-handle handle with moduleSetTag '${updatedModuleSetTag}'"
97             registerCmHandle(DMI1_URL, cmHandleIdWithExistingModuleSetTag, updatedModuleSetTag)
98             assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(cmHandleIdWithExistingModuleSetTag).moduleName.sort()
99         and: "a CM-handle with moduleSetTag '${initialModuleSetTag}' which will be upgraded"
100             registerCmHandle(DMI1_URL, cmHandleId, initialModuleSetTag)
101             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(cmHandleId).moduleName.sort()
102
103         when: "CM-handle is upgraded to moduleSetTag '${updatedModuleSetTag}'"
104             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [cmHandleId], moduleSetTag: updatedModuleSetTag)
105             def dmiPluginRegistrationResponse = objectUnderTest.updateDmiRegistration(
106                     new DmiPluginRegistration(dmiPlugin: DMI1_URL, upgradedCmHandles: cmHandlesToUpgrade))
107
108         then: 'registration gives successful response'
109             assert dmiPluginRegistrationResponse.upgradedCmHandles == [CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)]
110
111         and: 'the module sync watchdog is triggered twice'
112             2.times { moduleSyncWatchdog.moduleSyncAdvisedCmHandles() }
113
114         and: 'CM-handle goes to READY state'
115             new PollingConditions().within(MODULE_SYNC_WAIT_TIME_IN_SECONDS, () -> {
116                 assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(cmHandleId).cmHandleState
117             })
118
119         and: 'the CM-handle has expected moduleSetTag'
120             assert objectUnderTest.getNcmpServiceCmHandle(cmHandleId).moduleSetTag == updatedModuleSetTag
121
122         and: 'CM-handle has expected updated modules: M1 and M3'
123             assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(cmHandleId).moduleName.sort()
124
125         cleanup: 'deregister CM-handle'
126             deregisterCmHandles(DMI1_URL, [cmHandleId, cmHandleIdWithExistingModuleSetTag])
127
128         where:
129             initialModuleSetTag | updatedModuleSetTag
130             NO_MODULE_SET_TAG   | 'module@Set2'
131             'module@Set1'       | 'module@Set2'
132     }
133
134     def 'Skip upgrade of CM-handle with same moduleSetTag as before.'() {
135         given: 'an existing CM-handle with expected initial modules: M1 and M2'
136             dmiDispatcher1.moduleNamesPerCmHandleId[cmHandleId] = ['M1', 'M2']
137             registerCmHandle(DMI1_URL, cmHandleId, 'same')
138             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(cmHandleId).moduleName.sort()
139
140         when: 'CM-handle is upgraded with the same moduleSetTag'
141             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [cmHandleId], moduleSetTag: 'same')
142             objectUnderTest.updateDmiRegistration(
143                     new DmiPluginRegistration(dmiPlugin: DMI1_URL, upgradedCmHandles: cmHandlesToUpgrade))
144
145         then: 'CM-handle remains in READY state'
146             assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(cmHandleId).cmHandleState
147
148         and: 'the CM-handle has same moduleSetTag as before'
149             assert objectUnderTest.getNcmpServiceCmHandle(cmHandleId).moduleSetTag == 'same'
150
151         then: 'CM-handle has same modules as before: M1 and M2'
152             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(cmHandleId).moduleName.sort()
153
154         cleanup: 'deregister CM-handle'
155             deregisterCmHandle(DMI1_URL, cmHandleId)
156     }
157
158     def 'Upgrade of CM-handle fails due to DMI error.'() {
159         given: 'a CM-handle exists'
160             dmiDispatcher1.moduleNamesPerCmHandleId[cmHandleId] = ['M1', 'M2']
161             registerCmHandle(DMI1_URL, cmHandleId, 'oldTag')
162         and: 'DMI is not available for upgrade'
163             dmiDispatcher1.isAvailable = false
164
165         when: 'the CM-handle is upgraded'
166             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [cmHandleId], moduleSetTag: 'newTag')
167             objectUnderTest.updateDmiRegistration(
168                     new DmiPluginRegistration(dmiPlugin: DMI1_URL, upgradedCmHandles: cmHandlesToUpgrade))
169
170         and: 'the module sync watchdog is triggered twice'
171             2.times { moduleSyncWatchdog.moduleSyncAdvisedCmHandles() }
172
173         then: 'CM-handle goes to LOCKED state with reason MODULE_UPGRADE_FAILED'
174             new PollingConditions().within(MODULE_SYNC_WAIT_TIME_IN_SECONDS, () -> {
175                 def cmHandleCompositeState = objectUnderTest.getCmHandleCompositeState(cmHandleId)
176                 assert cmHandleCompositeState.cmHandleState == CmHandleState.LOCKED
177                 assert cmHandleCompositeState.lockReason.lockReasonCategory == LockReasonCategory.MODULE_UPGRADE_FAILED
178             })
179
180         and: 'the CM-handle has same moduleSetTag as before'
181             assert objectUnderTest.getNcmpServiceCmHandle(cmHandleId).moduleSetTag == 'oldTag'
182
183         cleanup: 'deregister CM-handle'
184             deregisterCmHandle(DMI1_URL, cmHandleId)
185     }
186
187 }