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.lang.Ignore
32 import spock.util.concurrent.PollingConditions
34 import static org.springframework.test.web.client.match.MockRestRequestMatchers.anything
35 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus
37 class NcmpCmHandleUpgradeSpec extends CpsIntegrationSpecBase {
39 NetworkCmProxyDataService objectUnderTest
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'
50 objectUnderTest = networkCmProxyDataService
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()
59 and: 'DMI returns different modules for upgrade'
60 mockDmiResponsesForRegistration(DMI_URL, CM_HANDLE_ID, UPDATED_MODULE_REFERENCES_RESPONSE, UPDATED_MODULE_RESOURCES_RESPONSE)
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))
67 then: 'registration gives successful response'
68 assert dmiPluginRegistrationResponse.upgradedCmHandles == [CmHandleRegistrationResponse.createSuccessResponse(CM_HANDLE_ID)]
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}"
76 when: 'module sync runs'
77 moduleSyncWatchdog.resetPreviouslyFailedCmHandles()
78 moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
80 then: 'CM-handle goes to READY state'
81 new PollingConditions().within(3, () -> {
82 assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
85 and: 'CM-handle has expected updated modules: M1 and M3'
86 assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
88 and: 'DMI received expected requests'
89 mockDmiServer.verify()
91 cleanup: 'deregister CM-handle'
92 deregisterCmHandle(DMI_URL, CM_HANDLE_ID)
95 initialModuleSetTag | updatedModuleSetTag
96 NO_MODULE_SET_TAG | NO_MODULE_SET_TAG
97 NO_MODULE_SET_TAG | 'new'
98 'initial' | NO_MODULE_SET_TAG
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()
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))
115 then: 'registration gives successful response'
116 assert dmiPluginRegistrationResponse.upgradedCmHandles == [CmHandleRegistrationResponse.createSuccessResponse(CM_HANDLE_ID)]
118 when: 'module sync runs'
119 moduleSyncWatchdog.resetPreviouslyFailedCmHandles()
120 moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
122 then: 'CM-handle goes to READY state'
123 new PollingConditions().within(3, () -> {
124 assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
127 and: 'CM-handle has expected updated modules: M1 and M3'
128 assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
130 cleanup: 'deregister CM-handle'
131 deregisterCmHandles(DMI_URL, [CM_HANDLE_ID, CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG])
134 initialModuleSetTag | updatedModuleSetTag
135 NO_MODULE_SET_TAG | 'moduleSet2'
136 'moduleSet1' | 'moduleSet2'
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()
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))
150 then: 'CM-handle remains in READY state'
151 assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
153 and: 'CM-handle has same modules as before: M1 and M2'
154 assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
156 cleanup: 'deregister CM-handle'
157 deregisterCmHandle(DMI_URL, CM_HANDLE_ID)
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)
164 and: 'DMI returns error code'
165 mockDmiServer.expect(anything()).andRespond(withStatus(HttpStatus.SERVICE_UNAVAILABLE))
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))
172 and: 'module sync runs'
173 moduleSyncWatchdog.resetPreviouslyFailedCmHandles()
174 moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
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
183 cleanup: 'deregister CM-handle'
184 deregisterCmHandle(DMI_URL, CM_HANDLE_ID)