2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation
4 * Modifications Copyright (C) 2022 Bell Canada
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.impl
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import org.onap.cps.api.CpsDataService
26 import org.onap.cps.api.CpsModuleService
27 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService
28 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler
29 import org.onap.cps.ncmp.api.impl.exception.DmiRequestException
30 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
31 import org.onap.cps.ncmp.api.inventory.CmHandleQueries
32 import org.onap.cps.ncmp.api.inventory.CmHandleState
33 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
34 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse
35 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
36 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
37 import org.onap.cps.spi.exceptions.AlreadyDefinedExceptionBatch
38 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
39 import org.onap.cps.spi.exceptions.DataValidationException
40 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
41 import org.onap.cps.utils.JsonObjectMapper
42 import spock.lang.Shared
43 import spock.lang.Specification
45 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_DOES_NOT_EXIST
46 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_ALREADY_EXIST
47 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_INVALID_ID
48 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.UNKNOWN_ERROR
49 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.Status
50 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
52 class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification {
55 def ncmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: 'some-cm-handle-id')
57 def mockCpsModuleService = Mock(CpsModuleService)
58 def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
59 def mockDmiDataOperations = Mock(DmiDataOperations)
60 def mockNetworkCmProxyDataServicePropertyHandler = Mock(NetworkCmProxyDataServicePropertyHandler)
61 def mockInventoryPersistence = Mock(InventoryPersistence)
62 def mockCmhandleQueries = Mock(CmHandleQueries)
63 def stubbedNetworkCmProxyCmHandlerQueryService = Stub(NetworkCmProxyCmHandlerQueryService)
64 def mockLcmEventsCmHandleStateHandler = Mock(LcmEventsCmHandleStateHandler)
65 def mockCpsDataService = Mock(CpsDataService)
66 def objectUnderTest = getObjectUnderTest()
68 def 'DMI Registration: Create, Update & Delete operations are processed in the right order'() {
69 given: 'a registration with operations of all three types'
70 def dmiRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
71 dmiRegistration.setCreatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-1', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
72 dmiRegistration.setUpdatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-2', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
73 dmiRegistration.setRemovedCmHandles(['cmhandle-2'])
74 when: 'registration is processed'
75 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiRegistration)
76 // Spock validated invocation order between multiple then blocks
77 then: 'cm-handles are removed first'
78 1 * objectUnderTest.parseAndRemoveCmHandlesInDmiRegistration(*_)
79 then: 'cm-handles are created'
80 1 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(*_)
81 then: 'cm-handles are updated'
82 1 * mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(*_)
85 def 'DMI Registration: Response from all operations types are in response'() {
86 given: 'a registration with operations of all three types'
87 def dmiRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
88 dmiRegistration.setCreatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-1', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
89 dmiRegistration.setUpdatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-2', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
90 dmiRegistration.setRemovedCmHandles(['cmhandle-2'])
91 and: 'update cm-handles can be processed successfully'
92 def updateResponses = [CmHandleRegistrationResponse.createSuccessResponse('cmhandle-2')]
93 mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(*_) >> updateResponses
94 and: 'create cm-handles can be processed successfully'
95 def createdResponses = [CmHandleRegistrationResponse.createSuccessResponse('cmhandle-1')]
96 objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(*_) >> createdResponses
97 and: 'delete cm-handles can be processed successfully'
98 def removeResponses = [CmHandleRegistrationResponse.createSuccessResponse('cmhandle-3')]
99 objectUnderTest.parseAndRemoveCmHandlesInDmiRegistration(*_) >> removeResponses
100 when: 'registration is processed'
101 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiRegistration)
102 then: 'response has values from all operations'
103 response.getRemovedCmHandles() == removeResponses
104 response.getCreatedCmHandles() == createdResponses
105 response.getUpdatedCmHandles() == updateResponses
108 def 'Create CM-handle Validation: Registration with valid Service names: #scenario'() {
109 given: 'a registration '
110 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: dmiPlugin, dmiModelPlugin: dmiModelPlugin,
111 dmiDataPlugin: dmiDataPlugin)
112 dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
113 when: 'update registration and sync module is called with correct DMI plugin information'
114 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
115 then: 'create cm handles registration and sync modules is called with the correct plugin information'
116 1 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
118 scenario | dmiPlugin | dmiModelPlugin | dmiDataPlugin
119 'combined DMI plugin' | 'service1' | '' | ''
120 'data & model DMI plugins' | '' | 'service1' | 'service2'
121 'data & model using same service' | '' | 'service1' | 'service1'
124 def 'Create CM-handle Validation: Invalid DMI plugin service name with #scenario'() {
125 given: 'a registration '
126 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: dmiPlugin, dmiModelPlugin: dmiModelPlugin,
127 dmiDataPlugin: dmiDataPlugin)
128 dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
129 when: 'registration is called with incorrect DMI plugin information'
130 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
131 then: 'a DMI Request Exception is thrown with correct message details'
132 def exceptionThrown = thrown(DmiRequestException.class)
133 assert exceptionThrown.getMessage().contains(expectedMessageDetails)
134 and: 'registration is not called'
135 0 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
137 scenario | dmiPlugin | dmiModelPlugin | dmiDataPlugin || expectedMessageDetails
138 'empty DMI plugins' | '' | '' | '' || 'No DMI plugin service names'
139 'blank DMI plugins' | ' ' | ' ' | ' ' || 'No DMI plugin service names'
140 'null DMI plugins' | null | null | null || 'No DMI plugin service names'
141 'all DMI plugins' | 'service1' | 'service2' | 'service3' || 'Cannot register combined plugin service name and other service names'
142 '(combined)DMI and Data Plugin' | 'service1' | '' | 'service2' || 'Cannot register combined plugin service name and other service names'
143 '(combined)DMI and model Plugin' | 'service1' | 'service2' | '' || 'Cannot register combined plugin service name and other service names'
144 'only model DMI plugin' | '' | 'service1' | '' || 'Cannot register just a Data or Model plugin service name'
145 'only data DMI plugin' | '' | '' | 'service1' || 'Cannot register just a Data or Model plugin service name'
148 def 'Create CM-Handle Successfully: #scenario.'() {
149 given: 'a registration without cm-handle properties'
150 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
151 dmiPluginRegistration.createdCmHandles = [new NcmpServiceCmHandle(cmHandleId: 'cmhandle', dmiProperties: dmiProperties, publicProperties: publicProperties)]
152 when: 'registration is updated'
153 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
154 then: 'a successful response is received'
155 response.getCreatedCmHandles().size() == 1
156 with(response.getCreatedCmHandles().get(0)) {
157 assert it.status == Status.SUCCESS
158 assert it.cmHandle == 'cmhandle'
160 and: 'state handler is invoked with the expected parameters'
161 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(_) >> {
164 def cmHandleStatePerCmHandle = (args[0] as Map)
165 cmHandleStatePerCmHandle.each {
166 assert (it.key.id == 'cmhandle'
167 && it.key.dmiServiceName == 'my-server'
168 && it.value == CmHandleState.ADVISED)
173 scenario | dmiProperties | publicProperties || expectedDmiProperties | expectedPublicProperties
174 'with dmi & public properties' | ['dmi-key': 'dmi-value'] | ['public-key': 'public-value'] || '[{"name":"dmi-key","value":"dmi-value"}]' | '[{"name":"public-key","value":"public-value"}]'
175 'with only public properties' | [:] | ['public-key': 'public-value'] || '[]' | '[{"name":"public-key","value":"public-value"}]'
176 'with only dmi properties' | ['dmi-key': 'dmi-value'] | [:] || '[{"name":"dmi-key","value":"dmi-value"}]' | '[]'
177 'without dmi & public properties' | [:] | [:] || '[]' | '[]'
180 def 'Create CM-Handle Multiple Requests: All cm-handles creation requests are processed with some failures'() {
181 given: 'a registration with three cm-handles to be created'
182 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
183 createdCmHandles: [new NcmpServiceCmHandle(cmHandleId: 'cmhandle1'),
184 new NcmpServiceCmHandle(cmHandleId: 'cmhandle2'),
185 new NcmpServiceCmHandle(cmHandleId: 'cmhandle3')])
186 and: 'cm-handle creation is successful for 1st and 3rd; failed for 2nd'
187 mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(*_) >> { throw new AlreadyDefinedExceptionBatch(['cmhandle2']) }
188 when: 'registration is updated to create cm-handles'
189 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
190 then: 'a response is received for all cm-handles'
191 response.getCreatedCmHandles().size() == 1
192 and: 'all cm-handles creation fails'
193 response.getCreatedCmHandles().each {
194 assert it.cmHandle == 'cmhandle2'
195 assert it.status == Status.FAILURE
196 assert it.registrationError == CM_HANDLE_ALREADY_EXIST
197 assert it.errorText == 'cm-handle already exists'
201 def 'Create CM-Handle Error Handling: Registration fails: #scenario'() {
202 given: 'a registration without cm-handle properties'
203 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
204 dmiPluginRegistration.createdCmHandles = [new NcmpServiceCmHandle(cmHandleId: cmHandleId)]
205 and: 'cm-handler registration fails: #scenario'
206 mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(*_) >> { throw exception }
207 when: 'registration is updated'
208 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
209 then: 'a failure response is received'
210 response.getCreatedCmHandles().size() == 1
211 with(response.getCreatedCmHandles().get(0)) {
212 assert it.status == Status.FAILURE
213 assert it.cmHandle == cmHandleId
214 assert it.registrationError == expectedError
215 assert it.errorText == expectedErrorText
218 scenario | cmHandleId | exception || expectedError | expectedErrorText
219 'cm-handle already exist' | 'cmhandle' | new AlreadyDefinedExceptionBatch([cmHandleId]) || CM_HANDLE_ALREADY_EXIST | 'cm-handle already exists'
220 'cm-handle has invalid name' | 'cm handle with space' | new DataValidationException("", "") || CM_HANDLE_INVALID_ID | 'cm-handle has an invalid character(s) in id'
221 'unknown exception while registering cm-handle' | 'cmhandle' | new RuntimeException('Failed') || UNKNOWN_ERROR | 'Failed'
224 def 'Update CM-Handle: Update Operation Response is added to the response'() {
225 given: 'a registration to update CmHandles'
226 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
227 updatedCmHandles: [{}])
228 and: 'cm-handle updates can be processed successfully'
229 def updateOperationResponse = [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-1'),
230 CmHandleRegistrationResponse.createFailureResponse('cm-handle-2', new Exception("Failed")),
231 CmHandleRegistrationResponse.createFailureResponse('cm-handle-3', CM_HANDLE_DOES_NOT_EXIST),
232 CmHandleRegistrationResponse.createFailureResponse('cm handle 4', CM_HANDLE_INVALID_ID)]
233 mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(_) >> updateOperationResponse
234 when: 'registration is updated'
235 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
236 then: 'the response contains updateOperationResponse'
237 assert response.getUpdatedCmHandles().size() == 4
238 assert response.getUpdatedCmHandles().containsAll(updateOperationResponse)
241 def 'Remove CmHandle Successfully: #scenario'() {
242 given: 'a registration'
243 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
244 removedCmHandles: ['cmhandle'])
246 mockCpsModuleService.deleteSchemaSet(_, 'cmhandle', CASCADE_DELETE_ALLOWED) >>
247 { if (!schemaSetExist) { throw new SchemaSetNotFoundException("", "") } }
248 when: 'registration is updated to delete cmhandle'
249 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
250 then: 'the cmHandle state is updated to "DELETING"'
251 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.DELETING)
252 and: 'method to delete relevant schema set is called once'
253 1 * mockInventoryPersistence.deleteSchemaSetWithCascade(_)
254 and: 'method to delete relevant list/list element is called once'
255 1 * mockInventoryPersistence.deleteListOrListElement(_)
256 and: 'successful response is received'
257 assert response.getRemovedCmHandles().size() == 1
258 with(response.getRemovedCmHandles().get(0)) {
259 assert it.status == Status.SUCCESS
260 assert it.cmHandle == 'cmhandle'
262 and: 'the cmHandle state is updated to "DELETED"'
263 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.DELETED)
265 scenario | schemaSetExist
266 'schema-set exists and can be deleted successfully' | true
267 'schema-set does not exist' | false
270 def 'Remove CmHandle: All cm-handles delete requests are processed'() {
271 given: 'a registration with three cm-handles to be deleted'
272 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
273 removedCmHandles: ['cmhandle1', 'cmhandle2', 'cmhandle3'])
274 and: 'cm-handle deletion is successful for 1st and 3rd; failed for 2nd'
275 mockInventoryPersistence.deleteListOrListElement(_) >> {} >> { throw new RuntimeException("Failed") } >> {}
276 when: 'registration is updated to delete cmhandles'
277 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
278 then: 'a response is received for all cm-handles'
279 response.getRemovedCmHandles().size() == 3
280 and: '1st and 3rd cm-handle deletes successfully'
281 with(response.getRemovedCmHandles().get(0)) {
282 assert it.status == Status.SUCCESS
283 assert it.cmHandle == 'cmhandle1'
285 with(response.getRemovedCmHandles().get(2)) {
286 assert it.status == Status.SUCCESS
287 assert it.cmHandle == 'cmhandle3'
289 and: '2nd cm-handle deletion fails'
290 with(response.getRemovedCmHandles().get(1)) {
291 assert it.status == Status.FAILURE
292 assert it.registrationError == UNKNOWN_ERROR
293 assert it.errorText == 'Failed'
294 assert it.cmHandle == 'cmhandle2'
298 def 'Remove CmHandle Error Handling: Schema Set Deletion failed'() {
299 given: 'a registration'
300 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
301 removedCmHandles: ['cmhandle'])
302 and: 'schema set deletion failed with unknown error'
303 mockInventoryPersistence.deleteSchemaSetWithCascade(_) >> { throw new RuntimeException('Failed') }
304 when: 'registration is updated to delete cmhandle'
305 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
306 then: 'no exception is thrown'
308 and: 'cm-handle is not deleted'
309 0 * mockInventoryPersistence.deleteListOrListElement(_)
310 and: 'the cmHandle state is not updated to "DELETED"'
311 0 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.DELETED)
312 and: 'a failure response is received'
313 assert response.getRemovedCmHandles().size() == 1
314 with(response.getRemovedCmHandles().get(0)) {
315 assert it.status == Status.FAILURE
316 assert it.cmHandle == 'cmhandle'
317 assert it.errorText == 'Failed'
318 assert it.registrationError == UNKNOWN_ERROR
322 def 'Remove CmHandle Error Handling: #scenario'() {
323 given: 'a registration'
324 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
325 removedCmHandles: ['cmhandle'])
326 and: 'cm-handle deletion throws exception'
327 mockInventoryPersistence.deleteListOrListElement(_) >> { throw deleteListElementException }
328 when: 'registration is updated to delete cmhandle'
329 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
330 then: 'a failure response is received'
331 assert response.getRemovedCmHandles().size() == 1
332 with(response.getRemovedCmHandles().get(0)) {
333 assert it.status == Status.FAILURE
334 assert it.cmHandle == 'cmhandle'
335 assert it.registrationError == expectedError
336 assert it.errorText == expectedErrorText
338 and: 'the cm handle state is not updated to "DELETED"'
339 0 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.DELETED)
341 scenario | cmHandleId | deleteListElementException || expectedError | expectedErrorText
342 'cm-handle does not exist' | 'cmhandle' | new DataNodeNotFoundException("", "", "") || CM_HANDLE_DOES_NOT_EXIST | 'cm-handle does not exist'
343 'cm-handle has invalid name' | 'cm handle with space' | new DataValidationException("", "") || CM_HANDLE_INVALID_ID | 'cm-handle has an invalid character(s) in id'
344 'an unexpected exception' | 'cmhandle' | new RuntimeException("Failed") || UNKNOWN_ERROR | 'Failed'
347 def getObjectUnderTest() {
348 return Spy(new NetworkCmProxyDataServiceImpl(spiedJsonObjectMapper, mockDmiDataOperations,
349 mockNetworkCmProxyDataServicePropertyHandler, mockInventoryPersistence, mockCmhandleQueries,
350 stubbedNetworkCmProxyCmHandlerQueryService, mockLcmEventsCmHandleStateHandler, mockCpsDataService))