18c7096b6684eea324235826d405ecd1144214d1
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
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.inventory
22
23 import org.onap.cps.integration.base.CpsIntegrationSpecBase
24 import org.onap.cps.ncmp.api.NcmpResponseStatus
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.NcmpServiceCmHandle
28 import org.onap.cps.ncmp.events.lcm.v1.LcmEvent
29 import org.onap.cps.ncmp.impl.NetworkCmProxyInventoryFacadeImpl
30
31 class CmHandleUpdateSpec extends CpsIntegrationSpecBase {
32
33     NetworkCmProxyInventoryFacadeImpl objectUnderTest
34
35     def setup() {
36         objectUnderTest = networkCmProxyInventoryFacade
37         subscribeAndClearPreviousMessages('test-group-for-update', 'ncmp-events')
38     }
39
40     def cleanup() {
41         kafkaConsumer.unsubscribe()
42         kafkaConsumer.close()
43     }
44
45     def 'Update of CM-handle with new or unchanged alternate ID succeeds.'() {
46         given: 'DMI will return modules when requested'
47             dmiDispatcher1.moduleNamesPerCmHandleId = ['ch-1': ['M1', 'M2']]
48         and: "existing CM-handle with alternate ID: $oldAlternateId"
49             registerCmHandle(DMI1_URL, 'ch-1', NO_MODULE_SET_TAG, oldAlternateId)
50
51         when: "CM-handle is registered for update with new alternate ID: $newAlternateId"
52             def cmHandleToUpdate = new NcmpServiceCmHandle(cmHandleId: 'ch-1', alternateId: newAlternateId)
53             def dmiPluginRegistrationResponse =
54                     objectUnderTest.updateDmiRegistration(new DmiPluginRegistration(dmiPlugin: DMI1_URL, updatedCmHandles: [cmHandleToUpdate]))
55
56         then: 'registration gives successful response'
57             assert dmiPluginRegistrationResponse.updatedCmHandles == [CmHandleRegistrationResponse.createSuccessResponse('ch-1')]
58
59         and: 'the CM-handle has expected alternate ID'
60             assert objectUnderTest.getNcmpServiceCmHandle('ch-1').alternateId == expectedAlternateId
61
62         cleanup: 'deregister CM handles'
63             deregisterCmHandle(DMI1_URL, 'ch-1')
64
65         where:
66             oldAlternateId | newAlternateId || expectedAlternateId
67             ''             | ''             || ''
68             ''             | 'new'          || 'new'
69             'old'          | 'old'          || 'old'
70             'old'          | null           || 'old'
71             'old'          | ''             || 'old'
72             'old'          | '  '           || 'old'
73     }
74
75     def 'Update of CM-handle with previously set alternate ID fails.'() {
76         given: 'DMI will return modules when requested'
77             dmiDispatcher1.moduleNamesPerCmHandleId = ['ch-1': ['M1', 'M2']]
78         and: 'existing CM-handle with alternate ID'
79             registerCmHandle(DMI1_URL, 'ch-1', NO_MODULE_SET_TAG, 'original')
80
81         when: 'a CM-handle is registered for update with new alternate ID'
82             def cmHandleToUpdate = new NcmpServiceCmHandle(cmHandleId: 'ch-1', alternateId: 'new')
83             def dmiPluginRegistrationResponse =
84                     objectUnderTest.updateDmiRegistration(new DmiPluginRegistration(dmiPlugin: DMI1_URL, updatedCmHandles: [cmHandleToUpdate]))
85
86         then: 'registration gives failure response, due to cm-handle already existing'
87             assert dmiPluginRegistrationResponse.updatedCmHandles == [CmHandleRegistrationResponse.createFailureResponse('ch-1', NcmpResponseStatus.CM_HANDLE_ALREADY_EXIST)]
88
89         and: 'the CM-handle still has the old alternate ID'
90             assert objectUnderTest.getNcmpServiceCmHandle('ch-1').alternateId == 'original'
91
92         cleanup: 'deregister CM handles'
93             deregisterCmHandle(DMI1_URL, 'ch-1')
94     }
95
96     def 'CM Handle registration to verify changes in data producer identifier'() {
97         given: 'DMI will return modules when requested'
98             def cmHandleId = 'ch-id-for-update'
99             dmiDispatcher1.moduleNamesPerCmHandleId[cmHandleId] = ['M1', 'M2']
100
101         when: 'a CM-handle is registered for creation'
102
103             def cmHandleToCreate = new NcmpServiceCmHandle(cmHandleId: cmHandleId)
104             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: DMI1_URL, createdCmHandles: [cmHandleToCreate])
105             def dmiPluginRegistrationResponse = objectUnderTest.updateDmiRegistration(dmiPluginRegistration)
106
107         then: 'registration gives successful response'
108             assert dmiPluginRegistrationResponse.createdCmHandles == [CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)]
109
110         then: 'the module sync watchdog is triggered'
111             moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
112
113         and: 'flush the latest cm handle registration events( state transition from NONE to ADVISED and ADVISED to READY)'
114             getLatestConsumerRecordsWithMaxPollOf1Second(2)
115
116         and: 'cm handle updated with the data producer identifier'
117             def cmHandleToUpdate = new NcmpServiceCmHandle(cmHandleId: cmHandleId, dataProducerIdentifier: 'my-data-producer-id')
118             def dmiPluginRegistrationForUpdate = new DmiPluginRegistration(dmiPlugin: DMI1_URL, updatedCmHandles: [cmHandleToUpdate])
119             def dmiPluginRegistrationResponseForUpdate = objectUnderTest.updateDmiRegistration(dmiPluginRegistrationForUpdate)
120
121         then: 'registration gives successful response'
122             assert dmiPluginRegistrationResponseForUpdate.updatedCmHandles == [CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)]
123
124         and: 'get the latest message'
125             def consumerRecords = getLatestConsumerRecordsWithMaxPollOf1Second(1)
126
127         and: 'the message has the updated data producer identifier'
128             def notificationMessages = []
129             for (def consumerRecord : consumerRecords) {
130                 notificationMessages.add(jsonObjectMapper.convertJsonString(consumerRecord.value().toString(), LcmEvent))
131             }
132             assert notificationMessages[0].event.cmHandleId.contains(cmHandleId)
133             assert notificationMessages[0].event.dataProducerIdentifier == 'my-data-producer-id'
134
135         cleanup: 'deregister CM handle'
136             deregisterCmHandle(DMI1_URL, cmHandleId)
137     }
138
139 }