64449371fe71170801052ca2a13a278723ae67ab
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 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.NetworkCmProxyInventoryFacade
25 import org.onap.cps.ncmp.api.inventory.models.CmHandleRegistrationResponse
26 import org.onap.cps.ncmp.api.inventory.models.DmiPluginRegistration
27 import org.onap.cps.ncmp.api.inventory.models.UpgradedCmHandles
28 import org.onap.cps.ncmp.impl.inventory.models.CmHandleState
29 import org.onap.cps.ncmp.impl.inventory.models.LockReasonCategory
30 import spock.util.concurrent.PollingConditions
31
32 class CmHandleUpgradeSpec extends CpsIntegrationSpecBase {
33
34     NetworkCmProxyInventoryFacade objectUnderTest
35
36     static final CM_HANDLE_ID = 'ch-1'
37     static final CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG = 'ch-2'
38
39     def setup() {
40         objectUnderTest = networkCmProxyInventoryFacade
41     }
42
43     def 'Upgrade CM-handle with new moduleSetTag or no moduleSetTag.'() {
44         given: 'a CM-handle is created with expected initial modules: M1 and M2'
45             dmiDispatcher1.moduleNamesPerCmHandleId[CM_HANDLE_ID] = ['M1', 'M2']
46             registerCmHandle(DMI1_URL, CM_HANDLE_ID, initialModuleSetTag)
47             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
48
49         when: "the CM-handle is upgraded with given moduleSetTag '${updatedModuleSetTag}'"
50             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], 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(CM_HANDLE_ID)]
56
57         and: 'CM-handle is in LOCKED state due to MODULE_UPGRADE'
58             def cmHandleCompositeState = objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID)
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[CM_HANDLE_ID] = ['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(CM_HANDLE_ID).cmHandleState
72             })
73
74         and: 'the CM-handle has expected moduleSetTag'
75             assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == updatedModuleSetTag
76
77         and: 'CM-handle has expected updated modules: M1 and M3'
78             assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
79
80         cleanup: 'deregister CM-handle'
81             deregisterCmHandle(DMI1_URL, CM_HANDLE_ID)
82
83         where:
84             initialModuleSetTag | updatedModuleSetTag
85             NO_MODULE_SET_TAG   | NO_MODULE_SET_TAG
86             NO_MODULE_SET_TAG   | 'new'
87             'initial'           | NO_MODULE_SET_TAG
88             'initial'           | 'new'
89     }
90
91     def 'Upgrade CM-handle with existing moduleSetTag.'() {
92         given: 'DMI will return modules for registration'
93             dmiDispatcher1.moduleNamesPerCmHandleId[CM_HANDLE_ID] = ['M1', 'M2']
94             dmiDispatcher1.moduleNamesPerCmHandleId[CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG] = ['M1', 'M3']
95         and: "an existing CM-handle handle with moduleSetTag '${updatedModuleSetTag}'"
96             registerCmHandle(DMI1_URL, CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG, updatedModuleSetTag)
97             assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG).moduleName.sort()
98         and: "a CM-handle with moduleSetTag '${initialModuleSetTag}' which will be upgraded"
99             registerCmHandle(DMI1_URL, CM_HANDLE_ID, initialModuleSetTag)
100             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
101
102         when: "CM-handle is upgraded to moduleSetTag '${updatedModuleSetTag}'"
103             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: updatedModuleSetTag)
104             def dmiPluginRegistrationResponse = objectUnderTest.updateDmiRegistration(
105                     new DmiPluginRegistration(dmiPlugin: DMI1_URL, upgradedCmHandles: cmHandlesToUpgrade))
106
107         then: 'registration gives successful response'
108             assert dmiPluginRegistrationResponse.upgradedCmHandles == [CmHandleRegistrationResponse.createSuccessResponse(CM_HANDLE_ID)]
109
110         and: 'the module sync watchdog is triggered twice'
111             2.times { moduleSyncWatchdog.moduleSyncAdvisedCmHandles() }
112
113         and: 'CM-handle goes to READY state'
114             new PollingConditions().within(MODULE_SYNC_WAIT_TIME_IN_SECONDS, () -> {
115                 assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
116             })
117
118         and: 'the CM-handle has expected moduleSetTag'
119             assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == updatedModuleSetTag
120
121         and: 'CM-handle has expected updated modules: M1 and M3'
122             assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
123
124         cleanup: 'deregister CM-handle'
125             deregisterCmHandles(DMI1_URL, [CM_HANDLE_ID, CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG])
126
127         where:
128             initialModuleSetTag | updatedModuleSetTag
129             NO_MODULE_SET_TAG   | 'moduleSet2'
130             'moduleSet1'        | 'moduleSet2'
131     }
132
133     def 'Skip upgrade of CM-handle with same moduleSetTag as before.'() {
134         given: 'an existing CM-handle with expected initial modules: M1 and M2'
135             dmiDispatcher1.moduleNamesPerCmHandleId[CM_HANDLE_ID] = ['M1', 'M2']
136             registerCmHandle(DMI1_URL, CM_HANDLE_ID, 'same')
137             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
138
139         when: 'CM-handle is upgraded with the same moduleSetTag'
140             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: 'same')
141             objectUnderTest.updateDmiRegistration(
142                     new DmiPluginRegistration(dmiPlugin: DMI1_URL, upgradedCmHandles: cmHandlesToUpgrade))
143
144         then: 'CM-handle remains in READY state'
145             assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
146
147         and: 'the CM-handle has same moduleSetTag as before'
148             assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == 'same'
149
150         then: 'CM-handle has same modules as before: M1 and M2'
151             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
152
153         cleanup: 'deregister CM-handle'
154             deregisterCmHandle(DMI1_URL, CM_HANDLE_ID)
155     }
156
157     def 'Upgrade of CM-handle fails due to DMI error.'() {
158         given: 'a CM-handle exists'
159             dmiDispatcher1.moduleNamesPerCmHandleId[CM_HANDLE_ID] = ['M1', 'M2']
160             registerCmHandle(DMI1_URL, CM_HANDLE_ID, 'oldTag')
161         and: 'DMI is not available for upgrade'
162             dmiDispatcher1.isAvailable = false
163
164         when: 'the CM-handle is upgraded'
165             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: 'newTag')
166             objectUnderTest.updateDmiRegistration(
167                     new DmiPluginRegistration(dmiPlugin: DMI1_URL, upgradedCmHandles: cmHandlesToUpgrade))
168
169         and: 'the module sync watchdog is triggered twice'
170             2.times { moduleSyncWatchdog.moduleSyncAdvisedCmHandles() }
171
172         then: 'CM-handle goes to LOCKED state with reason MODULE_UPGRADE_FAILED'
173             new PollingConditions().within(MODULE_SYNC_WAIT_TIME_IN_SECONDS, () -> {
174                 def cmHandleCompositeState = objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID)
175                 assert cmHandleCompositeState.cmHandleState == CmHandleState.LOCKED
176                 assert cmHandleCompositeState.lockReason.lockReasonCategory == LockReasonCategory.MODULE_UPGRADE_FAILED
177             })
178
179         and: 'the CM-handle has same moduleSetTag as before'
180             assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == 'oldTag'
181
182         cleanup: 'deregister CM-handle'
183             deregisterCmHandle(DMI1_URL, CM_HANDLE_ID)
184     }
185
186 }