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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.integration.functional
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.util.concurrent.PollingConditions
33 import static org.springframework.test.web.client.match.MockRestRequestMatchers.anything
34 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus
36 class NcmpCmHandleUpgradeSpec extends CpsIntegrationSpecBase {
38 NetworkCmProxyDataService objectUnderTest
40 static final INITIAL_MODULE_REFERENCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreAWithModules_M1_M2_Response.json')
41 static final INITIAL_MODULE_RESOURCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreAWithModules_M1_M2_ResourcesResponse.json')
42 static final UPDATED_MODULE_REFERENCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreBWithModules_M1_M3_Response.json')
43 static final UPDATED_MODULE_RESOURCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreBWithModules_M1_M3_ResourcesResponse.json')
44 static final NO_MODULE_SET_TAG = ''
45 static final CM_HANDLE_ID = 'ch-1'
46 static final CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG = 'ch-2'
49 objectUnderTest = networkCmProxyDataService
52 def 'Upgrade CM-handle with new moduleSetTag or no moduleSetTag.'() {
53 given: 'an existing CM-handle with expected initial modules: M1 and M2'
54 registerCmHandle(DMI_URL, CM_HANDLE_ID, initialModuleSetTag, INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE)
55 assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
57 and: 'DMI returns different modules for upgrade'
58 mockDmiResponsesForModuleSync(DMI_URL, CM_HANDLE_ID, UPDATED_MODULE_REFERENCES_RESPONSE, UPDATED_MODULE_RESOURCES_RESPONSE)
60 when: "CM-handle is upgraded with given moduleSetTag '${updatedModuleSetTag}'"
61 def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: updatedModuleSetTag)
62 def dmiPluginRegistrationResponse = networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
63 new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))
65 then: 'registration gives successful response'
66 assert dmiPluginRegistrationResponse.upgradedCmHandles == [CmHandleRegistrationResponse.createSuccessResponse(CM_HANDLE_ID)]
68 and: 'CM-handle is in LOCKED state due to MODULE_UPGRADE'
69 def cmHandleCompositeState = objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID)
70 assert cmHandleCompositeState.cmHandleState == CmHandleState.LOCKED
71 assert cmHandleCompositeState.lockReason.lockReasonCategory == LockReasonCategory.MODULE_UPGRADE
72 assert cmHandleCompositeState.lockReason.details == "Upgrade to ModuleSetTag: ${updatedModuleSetTag}"
74 when: 'module sync runs'
75 moduleSyncWatchdog.resetPreviouslyFailedCmHandles()
76 moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
78 then: 'CM-handle goes to READY state'
79 new PollingConditions().within(3, () -> {
80 assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
83 and: 'the CM-handle has expected moduleSetTag'
84 assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == updatedModuleSetTag
86 and: 'CM-handle has expected updated modules: M1 and M3'
87 assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
89 and: 'DMI received expected requests'
90 mockDmiServer.verify()
92 cleanup: 'deregister CM-handle'
93 deregisterCmHandle(DMI_URL, CM_HANDLE_ID)
96 initialModuleSetTag | updatedModuleSetTag
97 NO_MODULE_SET_TAG | NO_MODULE_SET_TAG
98 NO_MODULE_SET_TAG | 'new'
99 'initial' | NO_MODULE_SET_TAG
103 def 'Upgrade CM-handle with existing moduleSetTag.'() {
104 given: "an existing CM-handle handle with moduleSetTag '${updatedModuleSetTag}'"
105 registerCmHandle(DMI_URL, CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG, updatedModuleSetTag, UPDATED_MODULE_REFERENCES_RESPONSE, UPDATED_MODULE_RESOURCES_RESPONSE)
106 assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG).moduleName.sort()
107 and: "a CM-handle with moduleSetTag '${initialModuleSetTag}' which will be upgraded"
108 registerCmHandle(DMI_URL, CM_HANDLE_ID, initialModuleSetTag, INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE)
109 assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
111 when: "CM-handle is upgraded to moduleSetTag '${updatedModuleSetTag}'"
112 def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: updatedModuleSetTag)
113 def dmiPluginRegistrationResponse = networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
114 new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))
116 then: 'registration gives successful response'
117 assert dmiPluginRegistrationResponse.upgradedCmHandles == [CmHandleRegistrationResponse.createSuccessResponse(CM_HANDLE_ID)]
119 when: 'module sync runs'
120 moduleSyncWatchdog.resetPreviouslyFailedCmHandles()
121 moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
123 then: 'CM-handle goes to READY state'
124 new PollingConditions().within(3, () -> {
125 assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
128 and: 'the CM-handle has expected moduleSetTag'
129 assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == updatedModuleSetTag
131 and: 'CM-handle has expected updated modules: M1 and M3'
132 assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
134 cleanup: 'deregister CM-handle'
135 deregisterCmHandles(DMI_URL, [CM_HANDLE_ID, CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG])
138 initialModuleSetTag | updatedModuleSetTag
139 NO_MODULE_SET_TAG | 'moduleSet2'
140 'moduleSet1' | 'moduleSet2'
143 def 'Skip upgrade of CM-handle with same moduleSetTag as before.'() {
144 given: 'an existing CM-handle with expected initial modules: M1 and M2'
145 registerCmHandle(DMI_URL, CM_HANDLE_ID, 'same', INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE)
146 assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
148 when: 'CM-handle is upgraded with the same moduleSetTag'
149 def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: 'same')
150 networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
151 new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))
153 then: 'CM-handle remains in READY state'
154 assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
156 and: 'the CM-handle has same moduleSetTag as before'
157 assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == 'same'
159 then: 'CM-handle has same modules as before: M1 and M2'
160 assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
162 cleanup: 'deregister CM-handle'
163 deregisterCmHandle(DMI_URL, CM_HANDLE_ID)
166 def 'Upgrade of CM-handle fails due to DMI error.'() {
167 given: 'an existing CM-handle'
168 registerCmHandle(DMI_URL, CM_HANDLE_ID, 'oldTag', INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE)
170 and: 'DMI returns error code'
171 mockDmiServer.expect(anything()).andRespond(withStatus(HttpStatus.SERVICE_UNAVAILABLE))
173 when: "CM-handle is upgraded"
174 def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: 'newTag')
175 networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
176 new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))
178 and: 'module sync runs'
179 moduleSyncWatchdog.resetPreviouslyFailedCmHandles()
180 moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
182 then: 'CM-handle goes to LOCKED state with reason MODULE_UPGRADE_FAILED'
183 new PollingConditions().within(3, () -> {
184 def cmHandleCompositeState = objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID)
185 assert cmHandleCompositeState.cmHandleState == CmHandleState.LOCKED
186 assert cmHandleCompositeState.lockReason.lockReasonCategory == LockReasonCategory.MODULE_UPGRADE_FAILED
189 and: 'the CM-handle has same moduleSetTag as before'
190 assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == 'oldTag'
192 cleanup: 'deregister CM-handle'
193 deregisterCmHandle(DMI_URL, CM_HANDLE_ID)