2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2024 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 org.onap.cps.ncmp.api.impl.trustlevel.TrustLevelManager
25 import org.onap.cps.ncmp.api.impl.utils.CmHandleIdMapper
26 import org.onap.cps.ncmp.api.models.UpgradedCmHandles
28 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_FOUND
29 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_ALREADY_EXIST
30 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_INVALID_ID
31 import static org.onap.cps.ncmp.api.NcmpResponseStatus.UNKNOWN_ERROR
32 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.Status
34 import com.fasterxml.jackson.databind.ObjectMapper
35 import com.hazelcast.map.IMap
36 import org.onap.cps.api.CpsDataService
37 import org.onap.cps.api.CpsModuleService
38 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandleQueryService
39 import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler
40 import org.onap.cps.ncmp.api.impl.exception.DmiRequestException
41 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
42 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel
43 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
44 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries
45 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
46 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence
47 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse
48 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
49 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
50 import org.onap.cps.spi.exceptions.AlreadyDefinedException
51 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
52 import org.onap.cps.spi.exceptions.DataValidationException
53 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
54 import org.onap.cps.utils.JsonObjectMapper
55 import spock.lang.Shared
56 import spock.lang.Specification
58 class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification {
61 def ncmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: 'some-cm-handle-id')
63 def mockCpsModuleService = Mock(CpsModuleService)
64 def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
65 def mockDmiDataOperations = Mock(DmiDataOperations)
66 def mockNetworkCmProxyDataServicePropertyHandler = Mock(NetworkCmProxyDataServicePropertyHandler)
67 def mockInventoryPersistence = Mock(InventoryPersistence)
68 def mockCmHandleQueries = Mock(CmHandleQueries)
69 def stubbedNetworkCmProxyCmHandlerQueryService = Stub(NetworkCmProxyCmHandleQueryService)
70 def mockLcmEventsCmHandleStateHandler = Mock(LcmEventsCmHandleStateHandler)
71 def mockCpsDataService = Mock(CpsDataService)
72 def mockModuleSyncStartedOnCmHandles = Mock(IMap<String, Object>)
73 def trustLevelPerDmiPlugin = [:]
74 def mockTrustLevelManager = Mock(TrustLevelManager)
75 def mockCmHandleIdMapper = Mock(CmHandleIdMapper)
76 def objectUnderTest = getObjectUnderTest()
78 def 'DMI Registration: Create, Update, Delete & Upgrade operations are processed in the right order'() {
79 given: 'a registration with operations of all types'
80 def dmiRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
81 dmiRegistration.setCreatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-1', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
82 dmiRegistration.setUpdatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-2', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
83 dmiRegistration.setRemovedCmHandles(['cmhandle-2'])
84 dmiRegistration.setUpgradedCmHandles(new UpgradedCmHandles(cmHandles: ['cmhandle-3'], moduleSetTag: 'some-module-set-tag'))
85 and: 'cm handles are persisted'
86 mockInventoryPersistence.getYangModelCmHandles(['cmhandle-2']) >> [new YangModelCmHandle()]
87 and: 'cm handle is in READY state'
88 mockCmHandleQueries.cmHandleHasState('cmhandle-3', CmHandleState.READY) >> true
89 when: 'registration is processed'
90 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiRegistration)
91 then: 'cm-handles are removed first'
92 1 * objectUnderTest.parseAndProcessDeletedCmHandlesInRegistration(*_)
93 and: 'de-registered cm handle entry is removed from in progress map'
94 1 * mockModuleSyncStartedOnCmHandles.remove('cmhandle-2')
95 then: 'cm-handles are created'
96 1 * objectUnderTest.parseAndProcessCreatedCmHandlesInRegistration(*_)
97 then: 'cm-handles are updated'
98 1 * mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(*_)
99 then: 'cm-handles are upgraded'
100 1 * objectUnderTest.parseAndProcessUpgradedCmHandlesInRegistration(*_)
103 def 'DMI Registration upgrade operation with upgrade node state #scenario'() {
104 given: 'a registration with upgrade operation'
105 def dmiRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
106 dmiRegistration.setUpgradedCmHandles(new UpgradedCmHandles(cmHandles: ['cmhandle-3'], moduleSetTag: 'some-module-set-tag'))
107 and: 'exception while checking cm handle state'
108 mockCmHandleQueries.cmHandleHasState('cmhandle-3', CmHandleState.READY) >> isReady
109 when: 'registration is processed'
110 def result = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiRegistration)
111 then: 'upgrade operation contains expected error code'
112 assert result.upgradedCmHandles.status[0] == expectedResponseStatus
113 where: 'the following parameters are used'
114 scenario | isReady || expectedResponseStatus
115 'READY' | true || Status.SUCCESS
116 'Not READY' | false || Status.FAILURE
119 def 'DMI Registration upgrade with exception #scenario'() {
120 given: 'a registration with upgrade operation'
121 def dmiRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
122 dmiRegistration.setUpgradedCmHandles(new UpgradedCmHandles(cmHandles: ['cmhandle-3'], moduleSetTag: 'some-module-set-tag'))
123 and: 'exception while checking cm handle state'
124 mockCmHandleQueries.cmHandleHasState('cmhandle-3', CmHandleState.READY) >> { throw exception }
125 when: 'registration is processed'
126 def result = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiRegistration)
127 then: 'upgrade operation contains expected error code'
128 assert result.upgradedCmHandles.ncmpResponseStatus.code[0] == expectedErrorCode
129 where: 'the following parameters are used'
130 scenario | exception || expectedErrorCode
131 'data node not found' | new DataNodeNotFoundException('some-dataspace-name', 'some-anchor-name') || '100'
132 'cm handle is invalid' | new DataValidationException('some error message', 'some error details') || '110'
135 def 'DMI Registration: Response from all operations types are in response'() {
136 given: 'a registration with operations of all three types'
137 def dmiRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
138 dmiRegistration.setCreatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-1', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
139 dmiRegistration.setUpdatedCmHandles([new NcmpServiceCmHandle(cmHandleId: 'cmhandle-2', publicProperties: ['publicProp1': 'value'], dmiProperties: [:])])
140 dmiRegistration.setRemovedCmHandles(['cmhandle-2'])
141 and: 'update cm-handles can be processed successfully'
142 def updateResponses = [CmHandleRegistrationResponse.createSuccessResponse('cmhandle-2')]
143 mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(*_) >> updateResponses
144 and: 'create cm-handles can be processed successfully'
145 def createdResponses = [CmHandleRegistrationResponse.createSuccessResponse('cmhandle-1')]
146 objectUnderTest.parseAndProcessCreatedCmHandlesInRegistration(*_) >> createdResponses
147 and: 'delete cm-handles can be processed successfully'
148 def removeResponses = [CmHandleRegistrationResponse.createSuccessResponse('cmhandle-3')]
149 objectUnderTest.parseAndProcessDeletedCmHandlesInRegistration(*_) >> removeResponses
150 when: 'registration is processed'
151 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiRegistration)
152 then: 'response has values from all operations'
153 response.removedCmHandles == removeResponses
154 response.createdCmHandles == createdResponses
155 response.updatedCmHandles == updateResponses
158 def 'Create CM-handle Validation: Registration with valid Service names: #scenario'() {
159 given: 'a registration '
160 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: dmiPlugin, dmiModelPlugin: dmiModelPlugin,
161 dmiDataPlugin: dmiDataPlugin)
162 dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
163 when: 'update registration and sync module is called with correct DMI plugin information'
164 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
165 then: 'create cm handles registration and sync modules is called with the correct plugin information'
166 1 * objectUnderTest.parseAndProcessCreatedCmHandlesInRegistration(dmiPluginRegistration)
167 and: 'dmi is added to the dmi trustLevel map'
168 assert trustLevelPerDmiPlugin.size() == 1
169 assert trustLevelPerDmiPlugin.containsKey(expectedDmiPluginRegisteredName)
171 scenario | dmiPlugin | dmiModelPlugin | dmiDataPlugin || expectedDmiPluginRegisteredName
172 'combined DMI plugin' | 'service1' | '' | '' || 'service1'
173 'data & model DMI plugins' | '' | 'service1' | 'service2' || 'service2'
174 'data & model using same service' | '' | 'service1' | 'service1' || 'service1'
177 def 'Create CM-handle Validation: Invalid DMI plugin service name with #scenario'() {
178 given: 'a registration '
179 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: dmiPlugin, dmiModelPlugin: dmiModelPlugin,
180 dmiDataPlugin: dmiDataPlugin)
181 dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
182 when: 'registration is called with incorrect DMI plugin information'
183 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
184 then: 'a DMI Request Exception is thrown with correct message details'
185 def exceptionThrown = thrown(DmiRequestException.class)
186 assert exceptionThrown.getMessage().contains(expectedMessageDetails)
187 and: 'registration is not called'
188 0 * objectUnderTest.parseAndProcessCreatedCmHandlesInRegistration(dmiPluginRegistration)
190 scenario | dmiPlugin | dmiModelPlugin | dmiDataPlugin || expectedMessageDetails
191 'empty DMI plugins' | '' | '' | '' || 'No DMI plugin service names'
192 'blank DMI plugins' | ' ' | ' ' | ' ' || 'No DMI plugin service names'
193 'null DMI plugins' | null | null | null || 'No DMI plugin service names'
194 'all DMI plugins' | 'service1' | 'service2' | 'service3' || 'Cannot register combined plugin service name and other service names'
195 '(combined)DMI and Data Plugin' | 'service1' | '' | 'service2' || 'Cannot register combined plugin service name and other service names'
196 '(combined)DMI and model Plugin' | 'service1' | 'service2' | '' || 'Cannot register combined plugin service name and other service names'
197 'only model DMI plugin' | '' | 'service1' | '' || 'Cannot register just a Data or Model plugin service name'
198 'only data DMI plugin' | '' | '' | 'service1' || 'Cannot register just a Data or Model plugin service name'
201 def 'Create CM-Handle Successfully: #scenario.'() {
202 given: 'a registration without cm-handle properties'
203 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
204 dmiPluginRegistration.createdCmHandles = [new NcmpServiceCmHandle(cmHandleId: 'cmhandle', dmiProperties: dmiProperties, publicProperties: publicProperties)]
205 when: 'registration is updated'
206 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
207 then: 'a successful response is received'
208 response.createdCmHandles.size() == 1
209 with(response.createdCmHandles[0]) {
210 assert it.status == Status.SUCCESS
211 assert it.cmHandle == 'cmhandle'
213 and: 'state handler is invoked with the expected parameters'
214 1 * mockLcmEventsCmHandleStateHandler.initiateStateAdvised(_) >> {
216 def yangModelCmHandles = args[0]
217 assert yangModelCmHandles.id == ['cmhandle']
218 assert yangModelCmHandles.dmiServiceName == ['my-server']
222 scenario | dmiProperties | publicProperties || expectedDmiProperties | expectedPublicProperties
223 'with dmi & public properties' | ['dmi-key': 'dmi-value'] | ['public-key': 'public-value'] || '[{"name":"dmi-key","value":"dmi-value"}]' | '[{"name":"public-key","value":"public-value"}]'
224 'with only public properties' | [:] | ['public-key': 'public-value'] || [:] | '[{"name":"public-key","value":"public-value"}]'
225 'with only dmi properties' | ['dmi-key': 'dmi-value'] | [:] || '[{"name":"dmi-key","value":"dmi-value"}]' | [:]
226 'without dmi & public properties' | [:] | [:] || [:] | [:]
229 def 'Add CM-Handle to trustLevelPerCmHandle Successfully with: #scenario.'() {
230 given: 'a registration with trustLevel and populated cache'
231 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
232 dmiPluginRegistration.createdCmHandles = [new NcmpServiceCmHandle(cmHandleId: 'ch-1', registrationTrustLevel: TrustLevel.NONE),
233 new NcmpServiceCmHandle(cmHandleId: cmHandleId, registrationTrustLevel: registrationTrustLevel)]
234 when: 'registration is updated'
235 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
236 then: 'a successful response is received'
237 assert response.createdCmHandles.size() == expectedNumberOfCreatedCmHandles
238 and: 'trustLevel is set for the created cm-handle'
239 1 * mockTrustLevelManager.handleInitialRegistrationOfTrustLevels(_)
241 scenario | cmHandleId | registrationTrustLevel || expectedNumberOfCreatedCmHandles
242 'new trusted cm handle' | 'ch-new' | TrustLevel.COMPLETE || 2
243 'existing cm handle without trust level' | 'ch-1' | null || 1
244 'new cm handle without trust level' | 'ch-new' | null || 2
247 def 'Create CM-Handle Multiple Requests: All cm-handles creation requests are processed with some failures'() {
248 given: 'a registration with three cm-handles to be created'
249 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
250 createdCmHandles: [new NcmpServiceCmHandle(cmHandleId: 'cmhandle1'),
251 new NcmpServiceCmHandle(cmHandleId: 'cmhandle2'),
252 new NcmpServiceCmHandle(cmHandleId: 'cmhandle3')])
253 and: 'cm-handle creation is successful for 1st and 3rd; failed for 2nd'
254 def xpath = "somePathWithId[@id='cmhandle2']"
255 mockLcmEventsCmHandleStateHandler.initiateStateAdvised(*_) >> { throw AlreadyDefinedException.forDataNodes([xpath], 'some-context') }
256 when: 'registration is updated to create cm-handles'
257 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
258 then: 'a response is received for all cm-handles'
259 response.createdCmHandles.size() == 1
260 and: 'all cm-handles creation fails'
261 response.createdCmHandles.each {
262 assert it.cmHandle == 'cmhandle2'
263 assert it.status == Status.FAILURE
264 assert it.ncmpResponseStatus == CM_HANDLE_ALREADY_EXIST
265 assert it.errorText == 'cm-handle already exists'
269 def 'Create CM-Handle Error Handling: Registration fails: #scenario'() {
270 given: 'a registration without cm-handle properties'
271 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
272 dmiPluginRegistration.createdCmHandles = [new NcmpServiceCmHandle(cmHandleId: 'cmhandle')]
273 and: 'cm-handler registration fails: #scenario'
274 mockLcmEventsCmHandleStateHandler.initiateStateAdvised(*_) >> { throw exception }
275 when: 'registration is updated'
276 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
277 then: 'a failure response is received'
278 response.createdCmHandles.size() == 1
279 with(response.createdCmHandles[0]) {
280 assert it.status == Status.FAILURE
281 assert it.cmHandle == 'cmhandle'
282 assert it.ncmpResponseStatus == expectedError
283 assert it.errorText == expectedErrorText
286 scenario | exception || expectedError | expectedErrorText
287 'cm-handle already exist' | AlreadyDefinedException.forDataNodes(["path[@id='cmhandle']"], 'some-context') || CM_HANDLE_ALREADY_EXIST | 'cm-handle already exists'
288 'unknown exception while registering cm-handle' | new RuntimeException('Failed') || UNKNOWN_ERROR | 'Failed'
291 def 'Update CM-Handle: Update Operation Response is added to the response'() {
292 given: 'a registration to update CmHandles'
293 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server', updatedCmHandles: [{}])
294 and: 'cm-handle updates can be processed successfully'
295 def updateOperationResponse = [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-1'),
296 CmHandleRegistrationResponse.createFailureResponse('cm-handle-2', new Exception("Failed")),
297 CmHandleRegistrationResponse.createFailureResponse('cm-handle-3', CM_HANDLES_NOT_FOUND),
298 CmHandleRegistrationResponse.createFailureResponse('cm handle 4', CM_HANDLE_INVALID_ID)]
299 mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(_) >> updateOperationResponse
300 when: 'registration is updated'
301 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
302 then: 'the response contains updateOperationResponse'
303 assert response.updatedCmHandles.size() == 4
304 assert response.updatedCmHandles.containsAll(updateOperationResponse)
307 def 'Remove CmHandle Successfully: #scenario'() {
308 given: 'a registration'
309 addPersistedYangModelCmHandles(['cmhandle'])
310 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
311 removedCmHandles: ['cmhandle'])
313 mockCpsModuleService.deleteSchemaSetsWithCascade(_, ['cmhandle']) >>
314 { if (!schemaSetExist) { throw new SchemaSetNotFoundException("", "") } }
315 when: 'registration is updated to delete cmhandle'
316 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
317 then: 'the cmHandle state is updated to "DELETING"'
318 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(_)
319 and: 'method to delete relevant schema set is called once'
320 1 * mockInventoryPersistence.deleteSchemaSetsWithCascade(_)
321 and: 'method to delete relevant list/list element is called once'
322 1 * mockInventoryPersistence.deleteDataNodes(_)
323 and: 'successful response is received'
324 assert response.removedCmHandles.size() == 1
325 with(response.removedCmHandles[0]) {
326 assert it.status == Status.SUCCESS
327 assert it.cmHandle == 'cmhandle'
329 and: 'the cmHandle state is updated to "DELETED"'
330 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(_)
332 scenario | schemaSetExist
333 'schema-set exists and can be deleted successfully' | true
334 'schema-set does not exist' | false
337 def 'Remove CmHandle: Partial Success'() {
338 given: 'some unique yang model cm handles'
339 addPersistedYangModelCmHandles(['cmhandle1', 'cmhandle2', 'cmhandle3'])
340 and: 'a registration with three cm-handles to be deleted'
341 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
342 removedCmHandles: ['cmhandle1', 'cmhandle2', 'cmhandle3'])
343 and: 'cm-handle deletion fails on batch'
344 mockInventoryPersistence.deleteDataNodes(_) >> { throw new RuntimeException("Failed") }
345 and: 'cm-handle deletion is successful for 1st and 3rd; failed for 2nd'
346 mockInventoryPersistence.deleteDataNode("/dmi-registry/cm-handles[@id='cmhandle2']") >> { throw new RuntimeException("Failed") }
347 when: 'registration is updated to delete cmhandles'
348 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
349 then: 'the cmHandle states are all updated to "DELETING"'
350 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch({ assert it.every { entry -> entry.value == CmHandleState.DELETING } })
351 and: 'a response is received for all cm-handles'
352 response.removedCmHandles.size() == 3
353 and: 'successfully de-registered cm handle 1 is removed from in progress map'
354 1 * mockModuleSyncStartedOnCmHandles.remove('cmhandle1')
355 and: 'successfully de-registered cm handle 3 is removed from in progress map even though it was already being removed'
356 1 * mockModuleSyncStartedOnCmHandles.remove('cmhandle3') >> 'already in progress'
357 and: 'failed de-registered cm handle entries should not be removed from in progress map'
358 0 * mockModuleSyncStartedOnCmHandles.remove('cmhandle2')
359 and: '1st and 3rd cm-handle deletes successfully'
360 with(response.removedCmHandles[0]) {
361 assert it.status == Status.SUCCESS
362 assert it.cmHandle == 'cmhandle1'
364 with(response.removedCmHandles[2]) {
365 assert it.status == Status.SUCCESS
366 assert it.cmHandle == 'cmhandle3'
368 and: '2nd cm-handle deletion fails'
369 with(response.removedCmHandles[1]) {
370 assert it.status == Status.FAILURE
371 assert it.ncmpResponseStatus == UNKNOWN_ERROR
372 assert it.errorText == 'Failed'
373 assert it.cmHandle == 'cmhandle2'
375 and: 'the cmHandle state is updated to DELETED for 1st and 3rd'
376 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch({
377 assert it.size() == 2
378 assert it.every { entry -> entry.value == CmHandleState.DELETED }
382 def 'Remove CmHandle Error Handling: Schema Set Deletion failed'() {
383 given: 'a registration'
384 addPersistedYangModelCmHandles(['cmhandle'])
385 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
386 removedCmHandles: ['cmhandle'])
387 and: 'schema set batch deletion failed with unknown error'
388 mockInventoryPersistence.deleteSchemaSetsWithCascade(_) >> { throw new RuntimeException('Failed') }
389 and: 'schema set single deletion failed with unknown error'
390 mockInventoryPersistence.deleteSchemaSetWithCascade(_) >> { throw new RuntimeException('Failed') }
391 when: 'registration is updated to delete cmhandle'
392 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
393 then: 'no exception is thrown'
395 and: 'cm-handle is not deleted'
396 0 * mockInventoryPersistence.deleteDataNodes(_)
397 and: 'the cmHandle state is not updated to "DELETED"'
398 0 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch([yangModelCmHandle: CmHandleState.DELETED])
399 and: 'a failure response is received'
400 assert response.removedCmHandles.size() == 1
401 with(response.removedCmHandles[0]) {
402 assert it.status == Status.FAILURE
403 assert it.cmHandle == 'cmhandle'
404 assert it.errorText == 'Failed'
405 assert it.ncmpResponseStatus == UNKNOWN_ERROR
409 def 'Remove CmHandle Error Handling: #scenario'() {
410 given: 'a registration'
411 addPersistedYangModelCmHandles(['cmhandle'])
412 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
413 removedCmHandles: ['cmhandle'])
414 and: 'cm-handle deletion fails on batch'
415 mockInventoryPersistence.deleteDataNodes(_) >> { throw deleteListElementException }
416 and: 'cm-handle deletion fails on individual delete'
417 mockInventoryPersistence.deleteDataNode(_) >> { throw deleteListElementException }
418 when: 'registration is updated to delete cmhandle'
419 def response = objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
420 then: 'a failure response is received'
421 assert response.removedCmHandles.size() == 1
422 with(response.removedCmHandles[0]) {
423 assert it.status == Status.FAILURE
424 assert it.cmHandle == 'cmhandle'
425 assert it.ncmpResponseStatus == expectedError
426 assert it.errorText == expectedErrorText
428 and: 'the cm handle state is not updated to "DELETED"'
429 0 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(_, CmHandleState.DELETED)
431 scenario | cmHandleId | deleteListElementException || expectedError | expectedErrorText
432 'cm-handle does not exist' | 'cmhandle' | new DataNodeNotFoundException('', '', '') || CM_HANDLES_NOT_FOUND | 'cm handle id(s) not found'
433 'cm-handle has invalid name' | 'cm handle with space' | new DataValidationException('', '') || CM_HANDLE_INVALID_ID | 'cm-handle has an invalid character(s) in id'
434 'an unexpected exception' | 'cmhandle' | new RuntimeException('Failed') || UNKNOWN_ERROR | 'Failed'
437 def 'Adding data to alternate id caches.'() {
438 given: 'a registration with three CM Handles to be created'
439 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server',
440 createdCmHandles: [new NcmpServiceCmHandle(cmHandleId: 'cmhandle1', alternateId: 'my-alternate-id-1')])
441 when: 'the DMI plugin registration happens'
442 objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
443 then: 'the new alternate id is added to the cache'
444 1 * mockCmHandleIdMapper.addMapping('cmhandle1', 'my-alternate-id-1')
447 def getObjectUnderTest() {
448 return Spy(new NetworkCmProxyDataServiceImpl(spiedJsonObjectMapper, mockDmiDataOperations,
449 mockNetworkCmProxyDataServicePropertyHandler, mockInventoryPersistence, mockCmHandleQueries,
450 stubbedNetworkCmProxyCmHandlerQueryService, mockLcmEventsCmHandleStateHandler, mockCpsDataService,
451 mockModuleSyncStartedOnCmHandles, trustLevelPerDmiPlugin as Map<String, TrustLevel>, mockTrustLevelManager, mockCmHandleIdMapper))
454 def addPersistedYangModelCmHandles(ids) {
455 def yangModelCmHandles = ids.collect { new YangModelCmHandle(id:it) }
456 mockInventoryPersistence.getYangModelCmHandles(ids) >> yangModelCmHandles