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.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.AlreadyDefinedException
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 stubbedNetworkCmProxyCmHandlerQueryService = Stub(NetworkCmProxyCmHandlerQueryService)
63 def mockLcmEventsCmHandleStateHandler = Mock(LcmEventsCmHandleStateHandler)
64 def mockCpsDataService = Mock(CpsDataService)
65 def objectUnderTest = getObjectUnderTest()
67 def 'DMI Registration: Create, Update & Delete operations are processed in the right order'() {
68 given: 'a registration with operations of all three types'
69 def dmiRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
70 dmiRegistration.setCreatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-1', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
71 dmiRegistration.setUpdatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-2', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
72 dmiRegistration.setRemovedCmHandles(['cmhandle-2'])
73 when: 'registration is processed'
74 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiRegistration)
75 // Spock validated invocation order between multiple then blocks
76 then: 'cm-handles are removed first'
77 1 * objectUnderTest.parseAndRemoveCmHandlesInDmiRegistration(*_)
78 then: 'cm-handles are created'
79 1 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(*_)
80 then: 'cm-handles are updated'
81 1 * mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(*_)
84 def 'DMI Registration: Response from all operations types are in response'() {
85 given: 'a registration with operations of all three types'
86 def dmiRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
87 dmiRegistration.setCreatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-1', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
88 dmiRegistration.setUpdatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-2', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
89 dmiRegistration.setRemovedCmHandles(['cmhandle-2'])
90 and: 'update cm-handles can be processed successfully'
91 def updateResponses = [CmHandleRegistrationResponse.createSuccessResponse('cmhandle-2')]
92 mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(*_) >> updateResponses
93 and: 'create cm-handles can be processed successfully'
94 def createdResponses = [CmHandleRegistrationResponse.createSuccessResponse('cmhandle-1')]
95 objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(*_) >> createdResponses
96 and: 'delete cm-handles can be processed successfully'
97 def removeResponses = [CmHandleRegistrationResponse.createSuccessResponse('cmhandle-3')]
98 objectUnderTest.parseAndRemoveCmHandlesInDmiRegistration(*_) >> removeResponses
99 when: 'registration is processed'
100 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiRegistration)
101 then: 'response has values from all operations'
102 response.getRemovedCmHandles() == removeResponses
103 response.getCreatedCmHandles() == createdResponses
104 response.getUpdatedCmHandles() == updateResponses
107 def 'Create CM-handle Validation: Registration with valid Service names: #scenario'() {
108 given: 'a registration '
109 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: dmiPlugin, dmiModelPlugin: dmiModelPlugin,
110 dmiDataPlugin: dmiDataPlugin)
111 dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
112 when: 'update registration and sync module is called with correct DMI plugin information'
113 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
114 then: 'create cm handles registration and sync modules is called with the correct plugin information'
115 1 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
117 scenario | dmiPlugin | dmiModelPlugin | dmiDataPlugin
118 'combined DMI plugin' | 'service1' | '' | ''
119 'data & model DMI plugins' | '' | 'service1' | 'service2'
120 'data & model using same service' | '' | 'service1' | 'service1'
123 def 'Create CM-handle Validation: Invalid DMI plugin service name with #scenario'() {
124 given: 'a registration '
125 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: dmiPlugin, dmiModelPlugin: dmiModelPlugin,
126 dmiDataPlugin: dmiDataPlugin)
127 dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
128 when: 'registration is called with incorrect DMI plugin information'
129 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
130 then: 'a DMI Request Exception is thrown with correct message details'
131 def exceptionThrown = thrown(DmiRequestException.class)
132 assert exceptionThrown.getMessage().contains(expectedMessageDetails)
133 and: 'registration is not called'
134 0 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
136 scenario | dmiPlugin | dmiModelPlugin | dmiDataPlugin || expectedMessageDetails
137 'empty DMI plugins' | '' | '' | '' || 'No DMI plugin service names'
138 'blank DMI plugins' | ' ' | ' ' | ' ' || 'No DMI plugin service names'
139 'null DMI plugins' | null | null | null || 'No DMI plugin service names'
140 'all DMI plugins' | 'service1' | 'service2' | 'service3' || 'Cannot register combined plugin service name and other service names'
141 '(combined)DMI and Data Plugin' | 'service1' | '' | 'service2' || 'Cannot register combined plugin service name and other service names'
142 '(combined)DMI and model Plugin' | 'service1' | 'service2' | '' || 'Cannot register combined plugin service name and other service names'
143 'only model DMI plugin' | '' | 'service1' | '' || 'Cannot register just a Data or Model plugin service name'
144 'only data DMI plugin' | '' | '' | 'service1' || 'Cannot register just a Data or Model plugin service name'
147 def 'Create CM-Handle Successfully: #scenario.'() {
148 given: 'a registration without cm-handle properties'
149 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
150 dmiPluginRegistration.createdCmHandles = [new NcmpServiceCmHandle(cmHandleId: 'cmhandle', dmiProperties: dmiProperties, publicProperties: publicProperties)]
151 when: 'registration is updated'
152 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
153 then: 'a successful response is received'
154 response.getCreatedCmHandles().size() == 1
155 with(response.getCreatedCmHandles().get(0)) {
156 assert it.status == Status.SUCCESS
157 assert it.cmHandle == 'cmhandle'
159 and: 'state handler is invoked with the expected parameters'
160 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, _) >> {
162 def result = (args[0] as YangModelCmHandle)
163 assert result.id == 'cmhandle'
164 assert result.dmiServiceName == 'my-server'
165 assert CmHandleState.ADVISED == (args[1] as CmHandleState)
169 scenario | dmiProperties | publicProperties || expectedDmiProperties | expectedPublicProperties
170 'with dmi & public properties' | ['dmi-key': 'dmi-value'] | ['public-key': 'public-value'] || '[{"name":"dmi-key","value":"dmi-value"}]' | '[{"name":"public-key","value":"public-value"}]'
171 'with only public properties' | [:] | ['public-key': 'public-value'] || '[]' | '[{"name":"public-key","value":"public-value"}]'
172 'with only dmi properties' | ['dmi-key': 'dmi-value'] | [:] || '[{"name":"dmi-key","value":"dmi-value"}]' | '[]'
173 'without dmi & public properties' | [:] | [:] || '[]' | '[]'
177 def 'Create CM-Handle Multiple Requests: All cm-handles creation requests are processed'() {
178 given: 'a registration with three cm-handles to be created'
179 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
180 createdCmHandles: [new NcmpServiceCmHandle(cmHandleId: 'cmhandle1'),
181 new NcmpServiceCmHandle(cmHandleId: 'cmhandle2'),
182 new NcmpServiceCmHandle(cmHandleId: 'cmhandle3')])
183 and: 'cm-handle creation is successful for 1st and 3rd; failed for 2nd'
184 mockLcmEventsCmHandleStateHandler.updateCmHandleState(*_) >> {} >> { throw new RuntimeException("Failed") } >> {}
185 when: 'registration is updated to create cm-handles'
186 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
187 then: 'a response is received for all cm-handles'
188 response.getCreatedCmHandles().size() == 3
189 and: '1st and 3rd cm-handle are created successfully'
190 with(response.getCreatedCmHandles().get(0)) {
191 assert it.status == Status.SUCCESS
192 assert it.cmHandle == 'cmhandle1'
194 with(response.getCreatedCmHandles().get(2)) {
195 assert it.status == Status.SUCCESS
196 assert it.cmHandle == 'cmhandle3'
198 and: '2nd cm-handle creation fails'
199 with(response.getCreatedCmHandles().get(1)) {
200 assert it.status == Status.FAILURE
201 assert it.registrationError == UNKNOWN_ERROR
202 assert it.errorText == 'Failed'
203 assert it.cmHandle == 'cmhandle2'
207 def 'Create CM-Handle Error Handling: Registration fails: #scenario'() {
208 given: 'a registration without cm-handle properties'
209 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
210 dmiPluginRegistration.createdCmHandles = [new NcmpServiceCmHandle(cmHandleId: cmHandleId)]
211 and: 'cm-handler registration fails: #scenario'
212 mockLcmEventsCmHandleStateHandler.updateCmHandleState(*_) >> { throw exception }
213 when: 'registration is updated'
214 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
215 then: 'a failure response is received'
216 response.getCreatedCmHandles().size() == 1
217 with(response.getCreatedCmHandles().get(0)) {
218 assert it.status == Status.FAILURE
219 assert it.cmHandle == cmHandleId
220 assert it.registrationError == expectedError
221 assert it.errorText == expectedErrorText
224 scenario | cmHandleId | exception || expectedError | expectedErrorText
225 'cm-handle already exist' | 'cmhandle' | new AlreadyDefinedException('', new RuntimeException()) || CM_HANDLE_ALREADY_EXIST | 'cm-handle already exists'
226 'cm-handle has invalid name' | 'cm handle with space' | new DataValidationException("", "") || CM_HANDLE_INVALID_ID | 'cm-handle has an invalid character(s) in id'
227 'unknown exception while registering cm-handle' | 'cmhandle' | new RuntimeException('Failed') || UNKNOWN_ERROR | 'Failed'
230 def 'Update CM-Handle: Update Operation Response is added to the response'() {
231 given: 'a registration to update CmHandles'
232 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
233 updatedCmHandles: [{}])
234 and: 'cm-handle updates can be processed successfully'
235 def updateOperationResponse = [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-1'),
236 CmHandleRegistrationResponse.createFailureResponse('cm-handle-2', new Exception("Failed")),
237 CmHandleRegistrationResponse.createFailureResponse('cm-handle-3', CM_HANDLE_DOES_NOT_EXIST),
238 CmHandleRegistrationResponse.createFailureResponse('cm handle 4', CM_HANDLE_INVALID_ID)]
239 mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(_) >> updateOperationResponse
240 when: 'registration is updated'
241 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
242 then: 'the response contains updateOperationResponse'
243 assert response.getUpdatedCmHandles().size() == 4
244 assert response.getUpdatedCmHandles().containsAll(updateOperationResponse)
247 def 'Remove CmHandle Successfully: #scenario'() {
248 given: 'a registration'
249 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
250 removedCmHandles: ['cmhandle'])
252 mockCpsModuleService.deleteSchemaSet(_, 'cmhandle', CASCADE_DELETE_ALLOWED) >>
253 { if (!schemaSetExist) { throw new SchemaSetNotFoundException("", "") } }
254 when: 'registration is updated to delete cmhandle'
255 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
256 then: 'the cmHandle state is updated to "DELETING"'
257 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.DELETING)
258 and: 'method to delete relevant schema set is called once'
259 1 * mockInventoryPersistence.deleteSchemaSetWithCascade(_)
260 and: 'method to delete relevant list/list element is called once'
261 1 * mockInventoryPersistence.deleteListOrListElement(_)
262 and: 'successful response is received'
263 assert response.getRemovedCmHandles().size() == 1
264 with(response.getRemovedCmHandles().get(0)) {
265 assert it.status == Status.SUCCESS
266 assert it.cmHandle == 'cmhandle'
268 and: 'the cmHandle state is updated to "DELETED"'
269 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.DELETED)
271 scenario | schemaSetExist
272 'schema-set exists and can be deleted successfully' | true
273 'schema-set does not exist' | false
276 def 'Remove CmHandle: All cm-handles delete requests are processed'() {
277 given: 'a registration with three cm-handles to be deleted'
278 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
279 removedCmHandles: ['cmhandle1', 'cmhandle2', 'cmhandle3'])
280 and: 'cm-handle deletion is successful for 1st and 3rd; failed for 2nd'
281 mockInventoryPersistence.deleteListOrListElement(_) >> {} >> { throw new RuntimeException("Failed") } >> {}
282 when: 'registration is updated to delete cmhandles'
283 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
284 then: 'a response is received for all cm-handles'
285 response.getRemovedCmHandles().size() == 3
286 and: '1st and 3rd cm-handle deletes successfully'
287 with(response.getRemovedCmHandles().get(0)) {
288 assert it.status == Status.SUCCESS
289 assert it.cmHandle == 'cmhandle1'
291 with(response.getRemovedCmHandles().get(2)) {
292 assert it.status == Status.SUCCESS
293 assert it.cmHandle == 'cmhandle3'
295 and: '2nd cm-handle deletion fails'
296 with(response.getRemovedCmHandles().get(1)) {
297 assert it.status == Status.FAILURE
298 assert it.registrationError == UNKNOWN_ERROR
299 assert it.errorText == 'Failed'
300 assert it.cmHandle == 'cmhandle2'
304 def 'Remove CmHandle Error Handling: Schema Set Deletion failed'() {
305 given: 'a registration'
306 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
307 removedCmHandles: ['cmhandle'])
308 and: 'schema set deletion failed with unknown error'
309 mockInventoryPersistence.deleteSchemaSetWithCascade(_) >> { throw new RuntimeException('Failed') }
310 when: 'registration is updated to delete cmhandle'
311 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
312 then: 'no exception is thrown'
314 and: 'cm-handle is not deleted'
315 0 * mockInventoryPersistence.deleteListOrListElement(_)
316 and: 'the cmHandle state is not updated to "DELETED"'
317 0 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.DELETED)
318 and: 'a failure response is received'
319 assert response.getRemovedCmHandles().size() == 1
320 with(response.getRemovedCmHandles().get(0)) {
321 assert it.status == Status.FAILURE
322 assert it.cmHandle == 'cmhandle'
323 assert it.errorText == 'Failed'
324 assert it.registrationError == UNKNOWN_ERROR
328 def 'Remove CmHandle Error Handling: #scenario'() {
329 given: 'a registration'
330 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
331 removedCmHandles: ['cmhandle'])
332 and: 'cm-handle deletion throws exception'
333 mockInventoryPersistence.deleteListOrListElement(_) >> { throw deleteListElementException }
334 when: 'registration is updated to delete cmhandle'
335 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
336 then: 'a failure response is received'
337 assert response.getRemovedCmHandles().size() == 1
338 with(response.getRemovedCmHandles().get(0)) {
339 assert it.status == Status.FAILURE
340 assert it.cmHandle == 'cmhandle'
341 assert it.registrationError == expectedError
342 assert it.errorText == expectedErrorText
344 and: 'the cm handle state is not updated to "DELETED"'
345 0 * mockLcmEventsCmHandleStateHandler.updateCmHandleState(_, CmHandleState.DELETED)
347 scenario | cmHandleId | deleteListElementException || expectedError | expectedErrorText
348 'cm-handle does not exist' | 'cmhandle' | new DataNodeNotFoundException("", "", "") || CM_HANDLE_DOES_NOT_EXIST | 'cm-handle does not exist'
349 'cm-handle has invalid name' | 'cm handle with space' | new DataValidationException("", "") || CM_HANDLE_INVALID_ID | 'cm-handle has an invalid character(s) in id'
350 'an unexpected exception' | 'cmhandle' | new RuntimeException("Failed") || UNKNOWN_ERROR | 'Failed'
353 def getObjectUnderTest() {
354 return Spy(new NetworkCmProxyDataServiceImpl(spiedJsonObjectMapper, mockDmiDataOperations,
355 mockNetworkCmProxyDataServicePropertyHandler, mockInventoryPersistence, stubbedNetworkCmProxyCmHandlerQueryService,
356 mockLcmEventsCmHandleStateHandler, mockCpsDataService))