Tests of CM-handle module upgrade & moduleSetTag
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / functional / NcmpCmHandleUpgradeSpec.groovy
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
22
23 import org.onap.cps.integration.base.CpsIntegrationSpecBase
24 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
25 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
26 import org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory
27 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse
28 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
29 import org.onap.cps.ncmp.api.models.UpgradedCmHandles
30 import org.springframework.http.HttpStatus
31 import spock.lang.Ignore
32 import spock.util.concurrent.PollingConditions
33
34 import static org.springframework.test.web.client.match.MockRestRequestMatchers.anything
35 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus
36
37 class NcmpCmHandleUpgradeSpec extends CpsIntegrationSpecBase {
38
39     NetworkCmProxyDataService objectUnderTest
40
41     static final INITIAL_MODULE_REFERENCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreAWithModules_M1_M2_Response.json')
42     static final INITIAL_MODULE_RESOURCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreAWithModules_M1_M2_ResourcesResponse.json')
43     static final UPDATED_MODULE_REFERENCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreBWithModules_M1_M3_Response.json')
44     static final UPDATED_MODULE_RESOURCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreBWithModules_M1_M3_ResourcesResponse.json')
45     static final NO_MODULE_SET_TAG = ''
46     static final CM_HANDLE_ID = 'ch-1'
47     static final CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG = 'ch-2'
48
49     def setup() {
50         objectUnderTest = networkCmProxyDataService
51     }
52
53     @Ignore
54     def 'Upgrade CM-handle with new moduleSetTag or no moduleSetTag.'() {
55         given: 'an existing CM-handle with expected initial modules: M1 and M2'
56             registerCmHandle(DMI_URL, CM_HANDLE_ID, initialModuleSetTag, INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE)
57             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
58
59         and: 'DMI returns different modules for upgrade'
60             mockDmiResponsesForRegistration(DMI_URL, CM_HANDLE_ID, UPDATED_MODULE_REFERENCES_RESPONSE, UPDATED_MODULE_RESOURCES_RESPONSE)
61
62         when: "CM-handle is upgraded with given moduleSetTag '${updatedModuleSetTag}'"
63             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: updatedModuleSetTag)
64             def dmiPluginRegistrationResponse = networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
65                     new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))
66
67         then: 'registration gives successful response'
68             assert dmiPluginRegistrationResponse.upgradedCmHandles == [CmHandleRegistrationResponse.createSuccessResponse(CM_HANDLE_ID)]
69
70         and: 'CM-handle is in LOCKED state due to MODULE_UPGRADE'
71             def cmHandleCompositeState = objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID)
72             assert cmHandleCompositeState.cmHandleState == CmHandleState.LOCKED
73             assert cmHandleCompositeState.lockReason.lockReasonCategory == LockReasonCategory.MODULE_UPGRADE
74             assert cmHandleCompositeState.lockReason.details == "Upgrade to ModuleSetTag: ${updatedModuleSetTag}"
75
76         when: 'module sync runs'
77             moduleSyncWatchdog.resetPreviouslyFailedCmHandles()
78             moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
79
80         then: 'CM-handle goes to READY state'
81             new PollingConditions().within(3, () -> {
82                 assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
83             })
84
85         and: 'CM-handle has expected updated modules: M1 and M3'
86             assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
87
88         and: 'DMI received expected requests'
89             mockDmiServer.verify()
90
91         cleanup: 'deregister CM-handle'
92             deregisterCmHandle(DMI_URL, CM_HANDLE_ID)
93
94         where:
95             initialModuleSetTag | updatedModuleSetTag
96             NO_MODULE_SET_TAG   | NO_MODULE_SET_TAG
97             NO_MODULE_SET_TAG   | 'new'
98             'initial'           | NO_MODULE_SET_TAG
99             'initial'           | 'new'
100     }
101
102     def 'Upgrade CM-handle with existing moduleSetTag.'() {
103         given: "an existing CM-handle handle with moduleSetTag '${updatedModuleSetTag}'"
104             registerCmHandle(DMI_URL, CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG, updatedModuleSetTag, UPDATED_MODULE_REFERENCES_RESPONSE, UPDATED_MODULE_RESOURCES_RESPONSE)
105             assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG).moduleName.sort()
106         and: "a CM-handle with moduleSetTag '${initialModuleSetTag}' which will be upgraded"
107             registerCmHandle(DMI_URL, CM_HANDLE_ID, initialModuleSetTag, INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE)
108             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
109
110         when: "CM-handle is upgraded to moduleSetTag '${updatedModuleSetTag}'"
111             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: updatedModuleSetTag)
112             def dmiPluginRegistrationResponse = networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
113                     new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))
114
115         then: 'registration gives successful response'
116             assert dmiPluginRegistrationResponse.upgradedCmHandles == [CmHandleRegistrationResponse.createSuccessResponse(CM_HANDLE_ID)]
117
118         when: 'module sync runs'
119             moduleSyncWatchdog.resetPreviouslyFailedCmHandles()
120             moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
121
122         then: 'CM-handle goes to READY state'
123             new PollingConditions().within(3, () -> {
124                 assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
125             })
126
127         and: 'CM-handle has expected updated modules: M1 and M3'
128             assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
129
130         cleanup: 'deregister CM-handle'
131             deregisterCmHandles(DMI_URL, [CM_HANDLE_ID, CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG])
132
133         where:
134             initialModuleSetTag | updatedModuleSetTag
135             NO_MODULE_SET_TAG   | 'moduleSet2'
136             'moduleSet1'        | 'moduleSet2'
137     }
138
139     @Ignore
140     def 'Skip upgrade of CM-handle with same moduleSetTag as before.'() {
141         given: 'an existing CM-handle with expected initial modules: M1 and M2'
142             registerCmHandle(DMI_URL, CM_HANDLE_ID, 'same', INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE)
143             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
144
145         when: 'CM-handle is upgraded with the same moduleSetTag'
146             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: 'same')
147             networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
148                     new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))
149
150         then: 'CM-handle remains in READY state'
151             assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
152
153         and: 'CM-handle has same modules as before: M1 and M2'
154             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
155
156         cleanup: 'deregister CM-handle'
157             deregisterCmHandle(DMI_URL, CM_HANDLE_ID)
158     }
159
160     def 'Upgrade of CM-handle fails due to DMI error.'() {
161         given: 'an existing CM-handle'
162             registerCmHandle(DMI_URL, CM_HANDLE_ID, NO_MODULE_SET_TAG, INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE)
163
164         and: 'DMI returns error code'
165             mockDmiServer.expect(anything()).andRespond(withStatus(HttpStatus.SERVICE_UNAVAILABLE))
166
167         when: "CM-handle is upgraded"
168             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: NO_MODULE_SET_TAG)
169             networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
170                     new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))
171
172         and: 'module sync runs'
173             moduleSyncWatchdog.resetPreviouslyFailedCmHandles()
174             moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
175
176         then: 'CM-handle goes to LOCKED state with reason MODULE_UPGRADE_FAILED'
177             new PollingConditions().within(3, () -> {
178                 def cmHandleCompositeState = objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID)
179                 assert cmHandleCompositeState.cmHandleState == CmHandleState.LOCKED
180                 assert cmHandleCompositeState.lockReason.lockReasonCategory == LockReasonCategory.MODULE_UPGRADE_FAILED
181             })
182
183         cleanup: 'deregister CM-handle'
184             deregisterCmHandle(DMI_URL, CM_HANDLE_ID)
185     }
186
187 }