aboutsummaryrefslogtreecommitdiffstats
path: root/integration-test/src/test/groovy/org/onap/cps/integration/functional/NcmpCmHandleUpgradeSpec.groovy
blob: 5421ad32371d5f10f5fef7427cf10ed56402da04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 *  ============LICENSE_START=======================================================
 *  Copyright (C) 2024 Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the 'License');
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an 'AS IS' BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.onap.cps.integration.functional

import org.onap.cps.integration.base.CpsIntegrationSpecBase
import org.onap.cps.ncmp.api.NetworkCmProxyDataService
import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
import org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory
import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse
import org.onap.cps.ncmp.api.models.DmiPluginRegistration
import org.onap.cps.ncmp.api.models.UpgradedCmHandles
import org.springframework.http.HttpStatus
import spock.util.concurrent.PollingConditions

import static org.springframework.test.web.client.match.MockRestRequestMatchers.anything
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus

class NcmpCmHandleUpgradeSpec extends CpsIntegrationSpecBase {

    NetworkCmProxyDataService objectUnderTest

    static final INITIAL_MODULE_REFERENCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreAWithModules_M1_M2_Response.json')
    static final INITIAL_MODULE_RESOURCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreAWithModules_M1_M2_ResourcesResponse.json')
    static final UPDATED_MODULE_REFERENCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreBWithModules_M1_M3_Response.json')
    static final UPDATED_MODULE_RESOURCES_RESPONSE = readResourceDataFile('mock-dmi-responses/bookStoreBWithModules_M1_M3_ResourcesResponse.json')
    static final CM_HANDLE_ID = 'ch-1'
    static final CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG = 'ch-2'

    def setup() {
        objectUnderTest = networkCmProxyDataService
        mockDmiWillRespondToHealthChecks(DMI_URL)
    }

    def 'Upgrade CM-handle with new moduleSetTag or no moduleSetTag.'() {
        given: 'DMI will return modules for initial registration'
            mockDmiResponsesForModuleSync(DMI_URL, CM_HANDLE_ID, INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE)
        and: 'DMI returns different modules for upgrade'
            mockDmiResponsesForModuleSync(DMI_URL, CM_HANDLE_ID, UPDATED_MODULE_REFERENCES_RESPONSE, UPDATED_MODULE_RESOURCES_RESPONSE)

        when: 'a CM-handle is created with expected initial modules: M1 and M2'
            registerCmHandle(DMI_URL, CM_HANDLE_ID, initialModuleSetTag)
            assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
        and: "the CM-handle is upgraded with given moduleSetTag '${updatedModuleSetTag}'"
            def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: updatedModuleSetTag)
            def dmiPluginRegistrationResponse = networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
                    new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))

        then: 'registration gives successful response'
            assert dmiPluginRegistrationResponse.upgradedCmHandles == [CmHandleRegistrationResponse.createSuccessResponse(CM_HANDLE_ID)]

        and: 'CM-handle is in LOCKED state due to MODULE_UPGRADE'
            def cmHandleCompositeState = objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID)
            assert cmHandleCompositeState.cmHandleState == CmHandleState.LOCKED
            assert cmHandleCompositeState.lockReason.lockReasonCategory == LockReasonCategory.MODULE_UPGRADE
            assert cmHandleCompositeState.lockReason.details == "Upgrade to ModuleSetTag: ${updatedModuleSetTag}"

        when: 'module sync runs'
            moduleSyncWatchdog.resetPreviouslyFailedCmHandles()
            moduleSyncWatchdog.moduleSyncAdvisedCmHandles()

        then: 'CM-handle goes to READY state'
            new PollingConditions().within(3, () -> {
                assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
            })

        and: 'the CM-handle has expected moduleSetTag'
            assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == updatedModuleSetTag

        and: 'CM-handle has expected updated modules: M1 and M3'
            assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()

        and: 'DMI received expected requests'
            mockDmiServer.verify()

        cleanup: 'deregister CM-handle'
            deregisterCmHandle(DMI_URL, CM_HANDLE_ID)

        where:
            initialModuleSetTag | updatedModuleSetTag
            NO_MODULE_SET_TAG   | NO_MODULE_SET_TAG
            NO_MODULE_SET_TAG   | 'new'
            'initial'           | NO_MODULE_SET_TAG
            'initial'           | 'new'
    }

    def 'Upgrade CM-handle with existing moduleSetTag.'() {
        given: 'DMI will return modules for registration'
            mockDmiResponsesForModuleSync(DMI_URL, CM_HANDLE_ID, INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE)
            mockDmiResponsesForModuleSync(DMI_URL, CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG, UPDATED_MODULE_REFERENCES_RESPONSE, UPDATED_MODULE_RESOURCES_RESPONSE)
        and: "an existing CM-handle handle with moduleSetTag '${updatedModuleSetTag}'"
            registerCmHandle(DMI_URL, CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG, updatedModuleSetTag)
            assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG).moduleName.sort()
        and: "a CM-handle with moduleSetTag '${initialModuleSetTag}' which will be upgraded"
            registerCmHandle(DMI_URL, CM_HANDLE_ID, initialModuleSetTag)
            assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()

        when: "CM-handle is upgraded to moduleSetTag '${updatedModuleSetTag}'"
            def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: updatedModuleSetTag)
            def dmiPluginRegistrationResponse = networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
                    new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))

        then: 'registration gives successful response'
            assert dmiPluginRegistrationResponse.upgradedCmHandles == [CmHandleRegistrationResponse.createSuccessResponse(CM_HANDLE_ID)]

        when: 'module sync runs'
            moduleSyncWatchdog.resetPreviouslyFailedCmHandles()
            moduleSyncWatchdog.moduleSyncAdvisedCmHandles()

        then: 'CM-handle goes to READY state'
            new PollingConditions().within(3, () -> {
                assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
            })

        and: 'the CM-handle has expected moduleSetTag'
            assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == updatedModuleSetTag

        and: 'CM-handle has expected updated modules: M1 and M3'
            assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()

        cleanup: 'deregister CM-handle'
            deregisterCmHandles(DMI_URL, [CM_HANDLE_ID, CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG])

        where:
            initialModuleSetTag | updatedModuleSetTag
            NO_MODULE_SET_TAG   | 'moduleSet2'
            'moduleSet1'        | 'moduleSet2'
    }

    def 'Skip upgrade of CM-handle with same moduleSetTag as before.'() {
        given: 'an existing CM-handle with expected initial modules: M1 and M2'
            mockDmiResponsesForModuleSync(DMI_URL, CM_HANDLE_ID, INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE)
            registerCmHandle(DMI_URL, CM_HANDLE_ID, 'same')
            assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()

        when: 'CM-handle is upgraded with the same moduleSetTag'
            def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: 'same')
            networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
                    new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))

        then: 'CM-handle remains in READY state'
            assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState

        and: 'the CM-handle has same moduleSetTag as before'
            assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == 'same'

        then: 'CM-handle has same modules as before: M1 and M2'
            assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()

        cleanup: 'deregister CM-handle'
            deregisterCmHandle(DMI_URL, CM_HANDLE_ID)
    }

    def 'Upgrade of CM-handle fails due to DMI error.'() {
        given: 'DMI will return modules for initial registration'
            mockDmiResponsesForModuleSync(DMI_URL, CM_HANDLE_ID, INITIAL_MODULE_REFERENCES_RESPONSE, INITIAL_MODULE_RESOURCES_RESPONSE)
        and: 'DMI returns error code for upgrade'
            mockDmiServer.expect(anything()).andRespond(withStatus(HttpStatus.SERVICE_UNAVAILABLE))

        when: 'a CM-handle is created'
            registerCmHandle(DMI_URL, CM_HANDLE_ID, 'oldTag')
        and: 'the CM-handle is upgraded'
            def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: 'newTag')
            networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
                    new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))

        and: 'module sync runs'
            moduleSyncWatchdog.resetPreviouslyFailedCmHandles()
            moduleSyncWatchdog.moduleSyncAdvisedCmHandles()

        then: 'CM-handle goes to LOCKED state with reason MODULE_UPGRADE_FAILED'
            new PollingConditions().within(3, () -> {
                def cmHandleCompositeState = objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID)
                assert cmHandleCompositeState.cmHandleState == CmHandleState.LOCKED
                assert cmHandleCompositeState.lockReason.lockReasonCategory == LockReasonCategory.MODULE_UPGRADE_FAILED
            })

        and: 'the CM-handle has same moduleSetTag as before'
            assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == 'oldTag'

        cleanup: 'deregister CM-handle'
            deregisterCmHandle(DMI_URL, CM_HANDLE_ID)
    }

}