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.impl.yangmodels.YangModelCmHandle
32 import org.onap.cps.ncmp.api.inventory.CmHandleQueries
33 import org.onap.cps.ncmp.api.inventory.CmHandleState
34 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
35 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse
36 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
37 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
38 import org.onap.cps.spi.exceptions.AlreadyDefinedException
39 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
40 import org.onap.cps.spi.exceptions.DataValidationException
41 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
42 import org.onap.cps.utils.JsonObjectMapper
43 import spock.lang.Shared
44 import spock.lang.Specification
46 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_DOES_NOT_EXIST
47 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_ALREADY_EXIST
48 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_INVALID_ID
49 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.UNKNOWN_ERROR
50 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.Status
51 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
53 class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification {
56 def ncmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: 'some-cm-handle-id')
58 def mockCpsModuleService = Mock(CpsModuleService)
59 def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
60 def mockDmiDataOperations = Mock(DmiDataOperations)
61 def mockNetworkCmProxyDataServicePropertyHandler = Mock(NetworkCmProxyDataServicePropertyHandler)
62 def mockInventoryPersistence = Mock(InventoryPersistence)
63 def mockCmhandleQueries = Mock(CmHandleQueries)
64 def stubbedNetworkCmProxyCmHandlerQueryService = Stub(NetworkCmProxyCmHandlerQueryService)
65 def mockLcmEventsCmHandleStateHandler = Mock(LcmEventsCmHandleStateHandler)
66 def mockCpsDataService = Mock(CpsDataService)
67 def objectUnderTest = getObjectUnderTest()
69 def 'DMI Registration: Create, Update & Delete operations are processed in the right order'() {
70 given: 'a registration with operations of all three types'
71 def dmiRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
72 dmiRegistration.setCreatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-1', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
73 dmiRegistration.setUpdatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-2', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
74 dmiRegistration.setRemovedCmHandles(['cmhandle-2'])
75 when: 'registration is processed'
76 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiRegistration)
77 // Spock validated invocation order between multiple then blocks
78 then: 'cm-handles are removed first'
79 1 * objectUnderTest.parseAndRemoveCmHandlesInDmiRegistration(*_)
80 then: 'cm-handles are created'
81 1 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(*_)
82 then: 'cm-handles are updated'
83 1 * mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(*_)
86 def 'DMI Registration: Response from all operations types are in response'() {
87 given: 'a registration with operations of all three types'
88 def dmiRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
89 dmiRegistration.setCreatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-1', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
90 dmiRegistration.setUpdatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-2', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
91 dmiRegistration.setRemovedCmHandles(['cmhandle-2'])
92 and: 'update cm-handles can be processed successfully'
93 def updateResponses = [CmHandleRegistrationResponse.createSuccessResponse('cmhandle-2')]
94 mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(*_) >> updateResponses
95 and: 'create cm-handles can be processed successfully'
96 def createdResponses = [CmHandleRegistrationResponse.createSuccessResponse('cmhandle-1')]
97 objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(*_) >> createdResponses
98 and: 'delete cm-handles can be processed successfully'
99 def removeResponses = [CmHandleRegistrationResponse.createSuccessResponse('cmhandle-3')]
100 objectUnderTest.parseAndRemoveCmHandlesInDmiRegistration(*_) >> removeResponses
101 when: 'registration is processed'
102 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiRegistration)
103 then: 'response has values from all operations'
104 response.getRemovedCmHandles() == removeResponses
105 response.getCreatedCmHandles() == createdResponses
106 response.getUpdatedCmHandles() == updateResponses
109 def 'Create CM-handle Validation: Registration with valid Service names: #scenario'() {
110 given: 'a registration '
111 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: dmiPlugin, dmiModelPlugin: dmiModelPlugin,
112 dmiDataPlugin: dmiDataPlugin)
113 dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
114 when: 'update registration and sync module is called with correct DMI plugin information'
115 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
116 then: 'create cm handles registration and sync modules is called with the correct plugin information'
117 1 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
119 scenario | dmiPlugin | dmiModelPlugin | dmiDataPlugin
120 'combined DMI plugin' | 'service1' | '' | ''
121 'data & model DMI plugins' | '' | 'service1' | 'service2'
122 'data & model using same service' | '' | 'service1' | 'service1'
125 def 'Create CM-handle Validation: Invalid DMI plugin service name with #scenario'() {
126 given: 'a registration '
127 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: dmiPlugin, dmiModelPlugin: dmiModelPlugin,
128 dmiDataPlugin: dmiDataPlugin)
129 dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
130 when: 'registration is called with incorrect DMI plugin information'
131 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
132 then: 'a DMI Request Exception is thrown with correct message details'
133 def exceptionThrown = thrown(DmiRequestException.class)
134 assert exceptionThrown.getMessage().contains(expectedMessageDetails)
135 and: 'registration is not called'
136 0 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
138 scenario | dmiPlugin | dmiModelPlugin | dmiDataPlugin || expectedMessageDetails
139 'empty DMI plugins' | '' | '' | '' || 'No DMI plugin service names'
140 'blank DMI plugins' | ' ' | ' ' | ' ' || 'No DMI plugin service names'
141 'null DMI plugins' | null | null | null || 'No DMI plugin service names'
142 'all DMI plugins' | 'service1' | 'service2' | 'service3' || 'Cannot register combined plugin service name and other service names'
143 '(combined)DMI and Data Plugin' | 'service1' | '' | 'service2' || 'Cannot register combined plugin service name and other service names'
144 '(combined)DMI and model Plugin' | 'service1' | 'service2' | '' || 'Cannot register combined plugin service name and other service names'
145 'only model DMI plugin' | '' | 'service1' | '' || 'Cannot register just a Data or Model plugin service name'
146 'only data DMI plugin' | '' | '' | 'service1' || 'Cannot register just a Data or Model plugin service name'
149 def 'Create CM-Handle Successfully: #scenario.'() {
150 given: 'a registration without cm-handle properties'
151 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
152 dmiPluginRegistration.createdCmHandles = [new NcmpServiceCmHandle(cmHandleId: 'cmhandle', dmiProperties: dmiProperties, publicProperties: publicProperties)]
153 when: 'registration is updated'
154 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
155 then: 'a successful response is received'
156 response.getCreatedCmHandles().size() == 1
157 with(response.getCreatedCmHandles().get(0)) {
158 assert it.status == Status.SUCCESS
159 assert it.cmHandle == 'cmhandle'
161 and: 'state handler is invoked with the expected parameters'
162 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, _) >> {
164 def result = (args[0] as YangModelCmHandle)
165 assert result.id == 'cmhandle'
166 assert result.dmiServiceName == 'my-server'
167 assert CmHandleState.ADVISED == (args[1] as CmHandleState)
171 scenario | dmiProperties | publicProperties || expectedDmiProperties | expectedPublicProperties
172 'with dmi & public properties' | ['dmi-key': 'dmi-value'] | ['public-key': 'public-value'] || '[{"name":"dmi-key","value":"dmi-value"}]' | '[{"name":"public-key","value":"public-value"}]'
173 'with only public properties' | [:] | ['public-key': 'public-value'] || '[]' | '[{"name":"public-key","value":"public-value"}]'
174 'with only dmi properties' | ['dmi-key': 'dmi-value'] | [:] || '[{"name":"dmi-key","value":"dmi-value"}]' | '[]'
175 'without dmi & public properties' | [:] | [:] || '[]' | '[]'
179 def 'Create CM-Handle Multiple Requests: All cm-handles creation requests are processed'() {
180 given: 'a registration with three cm-handles to be created'
181 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
182 createdCmHandles: [new NcmpServiceCmHandle(cmHandleId: 'cmhandle1'),
183 new NcmpServiceCmHandle(cmHandleId: 'cmhandle2'),
184 new NcmpServiceCmHandle(cmHandleId: 'cmhandle3')])
185 and: 'cm-handle creation is successful for 1st and 3rd; failed for 2nd'
186 mockLcmEventsCmHandleStateHandler.updateCmHandleState(*_) >> {} >> { throw new RuntimeException("Failed") } >> {}
187 when: 'registration is updated to create cm-handles'
188 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
189 then: 'a response is received for all cm-handles'
190 response.getCreatedCmHandles().size() == 3
191 and: '1st and 3rd cm-handle are created successfully'
192 with(response.getCreatedCmHandles().get(0)) {
193 assert it.status == Status.SUCCESS
194 assert it.cmHandle == 'cmhandle1'
196 with(response.getCreatedCmHandles().get(2)) {
197 assert it.status == Status.SUCCESS
198 assert it.cmHandle == 'cmhandle3'
200 and: '2nd cm-handle creation fails'
201 with(response.getCreatedCmHandles().get(1)) {
202 assert it.status == Status.FAILURE
203 assert it.registrationError == UNKNOWN_ERROR
204 assert it.errorText == 'Failed'
205 assert it.cmHandle == 'cmhandle2'
209 def 'Create CM-Handle Error Handling: Registration fails: #scenario'() {
210 given: 'a registration without cm-handle properties'
211 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
212 dmiPluginRegistration.createdCmHandles = [new NcmpServiceCmHandle(cmHandleId: cmHandleId)]
213 and: 'cm-handler registration fails: #scenario'
214 mockLcmEventsCmHandleStateHandler.updateCmHandleState(*_) >> { throw exception }
215 when: 'registration is updated'
216 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
217 then: 'a failure response is received'
218 response.getCreatedCmHandles().size() == 1
219 with(response.getCreatedCmHandles().get(0)) {
220 assert it.status == Status.FAILURE
221 assert it.cmHandle == cmHandleId
222 assert it.registrationError == expectedError
223 assert it.errorText == expectedErrorText
226 scenario | cmHandleId | exception || expectedError | expectedErrorText
227 'cm-handle already exist' | 'cmhandle' | new AlreadyDefinedException('', new RuntimeException()) || CM_HANDLE_ALREADY_EXIST | 'cm-handle already exists'
228 'cm-handle has invalid name' | 'cm handle with space' | new DataValidationException("", "") || CM_HANDLE_INVALID_ID | 'cm-handle has an invalid character(s) in id'
229 'unknown exception while registering cm-handle' | 'cmhandle' | new RuntimeException('Failed') || UNKNOWN_ERROR | 'Failed'
232 def 'Update CM-Handle: Update Operation Response is added to the response'() {
233 given: 'a registration to update CmHandles'
234 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
235 updatedCmHandles: [{}])
236 and: 'cm-handle updates can be processed successfully'
237 def updateOperationResponse = [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-1'),
238 CmHandleRegistrationResponse.createFailureResponse('cm-handle-2', new Exception("Failed")),
239 CmHandleRegistrationResponse.createFailureResponse('cm-handle-3', CM_HANDLE_DOES_NOT_EXIST),
240 CmHandleRegistrationResponse.createFailureResponse('cm handle 4', CM_HANDLE_INVALID_ID)]
241 mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(_) >> updateOperationResponse
242 when: 'registration is updated'
243 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
244 then: 'the response contains updateOperationResponse'
245 assert response.getUpdatedCmHandles().size() == 4
246 assert response.getUpdatedCmHandles().containsAll(updateOperationResponse)
249 def 'Remove CmHandle Successfully: #scenario'() {
250 given: 'a registration'
251 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
252 removedCmHandles: ['cmhandle'])
254 mockCpsModuleService.deleteSchemaSet(_, 'cmhandle', CASCADE_DELETE_ALLOWED) >>
255 { if (!schemaSetExist) { throw new SchemaSetNotFoundException("", "") } }
256 when: 'registration is updated to delete cmhandle'
257 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
258 then: 'the cmHandle state is updated to "DELETING"'
259 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.DELETING)
260 and: 'method to delete relevant schema set is called once'
261 1 * mockInventoryPersistence.deleteSchemaSetWithCascade(_)
262 and: 'method to delete relevant list/list element is called once'
263 1 * mockInventoryPersistence.deleteListOrListElement(_)
264 and: 'successful response is received'
265 assert response.getRemovedCmHandles().size() == 1
266 with(response.getRemovedCmHandles().get(0)) {
267 assert it.status == Status.SUCCESS
268 assert it.cmHandle == 'cmhandle'
270 and: 'the cmHandle state is updated to "DELETED"'
271 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.DELETED)
273 scenario | schemaSetExist
274 'schema-set exists and can be deleted successfully' | true
275 'schema-set does not exist' | false
278 def 'Remove CmHandle: All cm-handles delete requests are processed'() {
279 given: 'a registration with three cm-handles to be deleted'
280 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
281 removedCmHandles: ['cmhandle1', 'cmhandle2', 'cmhandle3'])
282 and: 'cm-handle deletion is successful for 1st and 3rd; failed for 2nd'
283 mockInventoryPersistence.deleteListOrListElement(_) >> {} >> { throw new RuntimeException("Failed") } >> {}
284 when: 'registration is updated to delete cmhandles'
285 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
286 then: 'a response is received for all cm-handles'
287 response.getRemovedCmHandles().size() == 3
288 and: '1st and 3rd cm-handle deletes successfully'
289 with(response.getRemovedCmHandles().get(0)) {
290 assert it.status == Status.SUCCESS
291 assert it.cmHandle == 'cmhandle1'
293 with(response.getRemovedCmHandles().get(2)) {
294 assert it.status == Status.SUCCESS
295 assert it.cmHandle == 'cmhandle3'
297 and: '2nd cm-handle deletion fails'
298 with(response.getRemovedCmHandles().get(1)) {
299 assert it.status == Status.FAILURE
300 assert it.registrationError == UNKNOWN_ERROR
301 assert it.errorText == 'Failed'
302 assert it.cmHandle == 'cmhandle2'
306 def 'Remove CmHandle Error Handling: Schema Set Deletion failed'() {
307 given: 'a registration'
308 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
309 removedCmHandles: ['cmhandle'])
310 and: 'schema set deletion failed with unknown error'
311 mockInventoryPersistence.deleteSchemaSetWithCascade(_) >> { throw new RuntimeException('Failed') }
312 when: 'registration is updated to delete cmhandle'
313 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
314 then: 'no exception is thrown'
316 and: 'cm-handle is not deleted'
317 0 * mockInventoryPersistence.deleteListOrListElement(_)
318 and: 'the cmHandle state is not updated to "DELETED"'
319 0 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.DELETED)
320 and: 'a failure response is received'
321 assert response.getRemovedCmHandles().size() == 1
322 with(response.getRemovedCmHandles().get(0)) {
323 assert it.status == Status.FAILURE
324 assert it.cmHandle == 'cmhandle'
325 assert it.errorText == 'Failed'
326 assert it.registrationError == UNKNOWN_ERROR
330 def 'Remove CmHandle Error Handling: #scenario'() {
331 given: 'a registration'
332 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
333 removedCmHandles: ['cmhandle'])
334 and: 'cm-handle deletion throws exception'
335 mockInventoryPersistence.deleteListOrListElement(_) >> { throw deleteListElementException }
336 when: 'registration is updated to delete cmhandle'
337 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
338 then: 'a failure response is received'
339 assert response.getRemovedCmHandles().size() == 1
340 with(response.getRemovedCmHandles().get(0)) {
341 assert it.status == Status.FAILURE
342 assert it.cmHandle == 'cmhandle'
343 assert it.registrationError == expectedError
344 assert it.errorText == expectedErrorText
346 and: 'the cm handle state is not updated to "DELETED"'
347 0 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.DELETED)
349 scenario | cmHandleId | deleteListElementException || expectedError | expectedErrorText
350 'cm-handle does not exist' | 'cmhandle' | new DataNodeNotFoundException("", "", "") || CM_HANDLE_DOES_NOT_EXIST | 'cm-handle does not exist'
351 'cm-handle has invalid name' | 'cm handle with space' | new DataValidationException("", "") || CM_HANDLE_INVALID_ID | 'cm-handle has an invalid character(s) in id'
352 'an unexpected exception' | 'cmhandle' | new RuntimeException("Failed") || UNKNOWN_ERROR | 'Failed'
355 def getObjectUnderTest() {
356 return Spy(new NetworkCmProxyDataServiceImpl(spiedJsonObjectMapper, mockDmiDataOperations,
357 mockNetworkCmProxyDataServicePropertyHandler, mockInventoryPersistence, mockCmhandleQueries,
358 stubbedNetworkCmProxyCmHandlerQueryService, mockLcmEventsCmHandleStateHandler, mockCpsDataService))