2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation
4 * Modifications Copyright (C) 2021 Pantheon.tech
5 * Modifications Copyright (C) 2021-2022 Bell Canada
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.ncmp.api.impl
25 import com.hazelcast.map.IMap
26 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService
27 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler
28 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
29 import org.onap.cps.ncmp.api.inventory.CmHandleQueries
30 import org.onap.cps.ncmp.api.inventory.CmHandleState
31 import org.onap.cps.ncmp.api.inventory.CompositeState
32 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
33 import org.onap.cps.ncmp.api.inventory.LockReasonCategory
34 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState
35 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters
36 import org.onap.cps.ncmp.api.models.ConditionApiProperties
37 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
38 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
39 import org.onap.cps.spi.exceptions.CpsException
40 import org.onap.cps.spi.exceptions.DataValidationException
41 import org.onap.cps.spi.model.CmHandleQueryServiceParameters
42 import spock.lang.Shared
43 import java.util.stream.Collectors
44 import org.onap.cps.utils.JsonObjectMapper
45 import com.fasterxml.jackson.databind.ObjectMapper
46 import org.onap.cps.api.CpsDataService
47 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
48 import org.onap.cps.spi.FetchDescendantsOption
49 import org.onap.cps.spi.model.DataNode
50 import org.springframework.http.HttpStatus
51 import org.springframework.http.ResponseEntity
52 import spock.lang.Specification
54 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL
55 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING
56 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
57 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE
59 class NetworkCmProxyDataServiceImplSpec extends Specification {
61 def mockCpsDataService = Mock(CpsDataService)
62 def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
63 def mockDmiDataOperations = Mock(DmiDataOperations)
64 def nullNetworkCmProxyDataServicePropertyHandler = null
65 def mockInventoryPersistence = Mock(InventoryPersistence)
66 def mockCmHandleQueries = Mock(CmHandleQueries)
67 def mockDmiPluginRegistration = Mock(DmiPluginRegistration)
68 def mockCpsCmHandlerQueryService = Mock(NetworkCmProxyCmHandlerQueryService)
69 def mockLcmEventsCmHandleStateHandler = Mock(LcmEventsCmHandleStateHandler)
70 def stubModuleSyncStartedOnCmHandles = Stub(IMap<String, Object>)
73 def NO_REQUEST_ID = null
75 def OPTIONS_PARAM = '(a=1,b=2)'
77 def ncmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: 'test-cm-handle-id')
79 def objectUnderTest = new NetworkCmProxyDataServiceImpl(
80 spiedJsonObjectMapper,
81 mockDmiDataOperations,
82 nullNetworkCmProxyDataServicePropertyHandler,
83 mockInventoryPersistence,
85 mockCpsCmHandlerQueryService,
86 mockLcmEventsCmHandleStateHandler,
88 stubModuleSyncStartedOnCmHandles)
90 def cmHandleXPath = "/dmi-registry/cm-handles[@id='testCmHandle']"
92 def dataNode = new DataNode(leaves: ['id': 'some-cm-handle', 'dmi-service-name': 'testDmiService'])
94 def 'Write resource data for pass-through running from DMI using POST.'() {
95 given: 'cpsDataService returns valid datanode'
96 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
97 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
98 when: 'write resource data is called'
99 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
100 'testResourceId', CREATE,
101 '{some-json}', 'application/json')
102 then: 'DMI called with correct data'
103 1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
104 CREATE, '{some-json}', 'application/json')
105 >> { new ResponseEntity<>(HttpStatus.CREATED) }
108 def 'Write resource data for pass-through running from DMI using an invalid id.'() {
109 when: 'write resource data is called'
110 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('invalid cm handle name',
111 'testResourceId', CREATE,
112 '{some-json}', 'application/json')
113 then: 'exception is thrown'
114 thrown(DataValidationException.class)
115 and: 'DMI is not invoked'
116 0 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(_, _, _, _, _)
119 def 'Get resource data for pass-through operational from DMI.'() {
120 given: 'get data node is called'
121 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
122 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
123 and: 'get resource data from DMI is called'
124 mockDmiDataOperations.getResourceDataFromDmi(
128 PASSTHROUGH_OPERATIONAL,
130 NO_TOPIC) >> new ResponseEntity<>('dmi-response', HttpStatus.OK)
131 when: 'get resource data operational for cm-handle is called'
132 def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
137 then: 'DMI returns a json response'
138 response == 'dmi-response'
141 def 'Get resource data for pass-through running from DMI.'() {
142 given: 'cpsDataService returns valid data node'
143 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
144 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
145 and: 'DMI returns valid response and data'
146 mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
151 NO_TOPIC) >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
152 when: 'get resource data is called'
153 def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
158 then: 'get resource data returns expected response'
159 response == '{dmi-response}'
162 def 'Getting Yang Resources.'() {
163 when: 'yang resources is called'
164 objectUnderTest.getYangResourcesModuleReferences('some-cm-handle')
165 then: 'CPS module services is invoked for the correct dataspace and cm handle'
166 1 * mockInventoryPersistence.getYangResourcesModuleReferences('some-cm-handle')
169 def 'Getting Yang Resources with an invalid #scenario.'() {
170 when: 'yang resources is called'
171 objectUnderTest.getYangResourcesModuleReferences('invalid cm handle with spaces')
172 then: 'a data validation exception is thrown'
173 thrown(DataValidationException)
174 and: 'CPS module services is not invoked'
175 0 * mockInventoryPersistence.getYangResourcesModuleReferences(*_)
178 def 'Get a cm handle.'() {
179 given: 'the system returns a yang modelled cm handle'
180 def dmiServiceName = 'some service name'
181 def compositeState = new CompositeState(cmHandleState: CmHandleState.ADVISED,
182 lockReason: CompositeState.LockReason.builder().lockReasonCategory(LockReasonCategory.LOCKED_MODULE_SYNC_FAILED).details("lock details").build(),
183 lastUpdateTime: 'some-timestamp',
184 dataSyncEnabled: false,
185 dataStores: dataStores())
186 def dmiProperties = [new YangModelCmHandle.Property('Book', 'Romance Novel')]
187 def publicProperties = [new YangModelCmHandle.Property('Public Book', 'Public Romance Novel')]
188 def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', dmiServiceName: dmiServiceName,
189 dmiProperties: dmiProperties, publicProperties: publicProperties, compositeState: compositeState)
190 1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
191 when: 'getting cm handle details for a given cm handle id from ncmp service'
192 def result = objectUnderTest.getNcmpServiceCmHandle('some-cm-handle')
193 then: 'the result is a ncmpServiceCmHandle'
194 result.class == NcmpServiceCmHandle.class
195 and: 'the cm handle contains the cm handle id'
196 result.cmHandleId == 'some-cm-handle'
197 and: 'the cm handle contains the DMI Properties'
198 result.dmiProperties ==[ Book:'Romance Novel' ]
199 and: 'the cm handle contains the public Properties'
200 result.publicProperties == [ "Public Book":'Public Romance Novel' ]
201 and: 'the cm handle contains the cm handle composite state'
202 result.compositeState == compositeState
206 def 'Get a cm handle with an invalid id.'() {
207 when: 'getting cm handle details for a given cm handle id with an invalid name'
208 objectUnderTest.getNcmpServiceCmHandle('invalid cm handle with spaces')
209 then: 'an exception is thrown'
210 thrown(DataValidationException)
211 and: 'the yang model cm handle retriever is not invoked'
212 0 * mockInventoryPersistence.getYangModelCmHandle(*_)
215 def 'Get cm handle public properties'() {
216 given: 'a yang modelled cm handle'
217 def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
218 def publicProperties = [new YangModelCmHandle.Property('public prop', 'some public prop')]
219 def yangModelCmHandle = new YangModelCmHandle(id:'some-cm-handle', dmiServiceName: 'some service name', dmiProperties: dmiProperties, publicProperties: publicProperties)
220 and: 'the system returns this yang modelled cm handle'
221 1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
222 when: 'getting cm handle public properties for a given cm handle id from ncmp service'
223 def result = objectUnderTest.getCmHandlePublicProperties('some-cm-handle')
224 then: 'the result returns the correct data'
225 result == [ 'public prop' : 'some public prop' ]
228 def 'Get cm handle public properties with an invalid id.'() {
229 when: 'getting cm handle public properties for a given cm handle id with an invalid name'
230 objectUnderTest.getCmHandlePublicProperties('invalid cm handle with spaces')
231 then: 'an exception is thrown'
232 thrown(DataValidationException)
233 and: 'the yang model cm handle retriever is not invoked'
234 0 * mockInventoryPersistence.getYangModelCmHandle(*_)
237 def 'Get cm handle composite state'() {
238 given: 'a yang modelled cm handle'
239 def compositeState = new CompositeState(cmHandleState: CmHandleState.ADVISED,
240 lockReason: CompositeState.LockReason.builder().lockReasonCategory(LockReasonCategory.LOCKED_MODULE_SYNC_FAILED).details("lock details").build(),
241 lastUpdateTime: 'some-timestamp',
242 dataSyncEnabled: false,
243 dataStores: dataStores())
244 def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
245 def publicProperties = [new YangModelCmHandle.Property('public prop', 'some public prop')]
246 def yangModelCmHandle = new YangModelCmHandle(id:'some-cm-handle', dmiServiceName: 'some service name', dmiProperties: dmiProperties, publicProperties: publicProperties, compositeState: compositeState)
247 and: 'the system returns this yang modelled cm handle'
248 1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
249 when: 'getting cm handle composite state for a given cm handle id from ncmp service'
250 def result = objectUnderTest.getCmHandleCompositeState('some-cm-handle')
251 then: 'the result returns the correct data'
252 result == compositeState
255 def 'Get cm handle composite state with an invalid id.'() {
256 when: 'getting cm handle composite state for a given cm handle id with an invalid name'
257 objectUnderTest.getCmHandleCompositeState('invalid cm handle with spaces')
258 then: 'an exception is thrown'
259 thrown(DataValidationException)
260 and: 'the yang model cm handle retriever is not invoked'
261 0 * mockInventoryPersistence.getYangModelCmHandle(_)
264 def 'Update resource data for pass-through running from dmi using POST #scenario DMI properties.'() {
265 given: 'cpsDataService returns valid datanode'
266 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
267 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
268 when: 'get resource data is called'
269 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
270 'testResourceId', UPDATE,
271 '{some-json}', 'application/json')
272 then: 'DMI called with correct data'
273 1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
274 UPDATE, '{some-json}', 'application/json')
275 >> { new ResponseEntity<>(HttpStatus.OK) }
278 def 'Verify modules and create anchor params'() {
279 given: 'dmi plugin registration return created cm handles'
280 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'service1', dmiModelPlugin: 'service1',
281 dmiDataPlugin: 'service2')
282 dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
283 mockDmiPluginRegistration.getCreatedCmHandles() >> [ncmpServiceCmHandle]
284 when: 'parse and create cm handle in dmi registration then sync module'
285 objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(mockDmiPluginRegistration)
286 then: 'system persists the cm handle state'
287 1 * mockLcmEventsCmHandleStateHandler.updateCmHandleStateBatch(_) >> {
290 def cmHandleStatePerCmHandle = (args[0] as Map)
291 cmHandleStatePerCmHandle.each {
292 assert (it.key.id == 'test-cm-handle-id'
293 && it.value == CmHandleState.ADVISED)
299 def 'Execute cm handle id search'() {
300 given: 'valid CmHandleQueryApiParameters input'
301 def cmHandleQueryApiParameters = new CmHandleQueryApiParameters()
302 def conditionApiProperties = new ConditionApiProperties()
303 conditionApiProperties.conditionName = 'hasAllModules'
304 conditionApiProperties.conditionParameters = [[moduleName: 'module-name-1']]
305 cmHandleQueryApiParameters.cmHandleQueryParameters = [conditionApiProperties]
306 and: 'query cm handle method return with a data node list'
307 mockCpsCmHandlerQueryService.queryCmHandleIds(
308 spiedJsonObjectMapper.convertToValueType(cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class))
309 >> ['cm-handle-id-1']
310 when: 'execute cm handle search is called'
311 def result = objectUnderTest.executeCmHandleIdSearch(cmHandleQueryApiParameters)
312 then: 'result is the same collection as returned by the CPS Data Service'
313 assert result == ['cm-handle-id-1'] as Set
316 def 'Getting module definitions.'() {
317 when: 'get module definitions method is called with a valid cm handle ID'
318 objectUnderTest.getModuleDefinitionsByCmHandleId('some-cm-handle')
319 then: 'CPS module services is invoked once'
320 1 * mockInventoryPersistence.getModuleDefinitionsByCmHandleId('some-cm-handle')
323 def 'Execute cm handle search'() {
324 given: 'valid CmHandleQueryApiParameters input'
325 def cmHandleQueryApiParameters = new CmHandleQueryApiParameters()
326 def conditionApiProperties = new ConditionApiProperties()
327 conditionApiProperties.conditionName = 'hasAllModules'
328 conditionApiProperties.conditionParameters = [[moduleName: 'module-name-1']]
329 cmHandleQueryApiParameters.cmHandleQueryParameters = [conditionApiProperties]
330 and: 'query cm handle method return with a data node list'
331 mockCpsCmHandlerQueryService.queryCmHandles(
332 spiedJsonObjectMapper.convertToValueType(cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class))
333 >> [new NcmpServiceCmHandle(cmHandleId: 'cm-handle-id-1')]
334 when: 'execute cm handle search is called'
335 def result = objectUnderTest.executeCmHandleSearch(cmHandleQueryApiParameters)
336 then: 'result is the same collection as returned by the CPS Data Service'
337 assert result.stream().map(d -> d.cmHandleId).collect(Collectors.toSet()) == ['cm-handle-id-1'] as Set
340 def 'Set Cm Handle Data Sync Enabled Flag where data sync flag is #scenario'() {
341 given: 'an existing cm handle composite state'
342 def compositeState = new CompositeState(cmHandleState: CmHandleState.READY, dataSyncEnabled: initialDataSyncEnabledFlag,
343 dataStores: CompositeState.DataStores.builder()
344 .operationalDataStore(CompositeState.Operational.builder()
345 .dataStoreSyncState(initialDataSyncState)
347 and: 'get cm handle state returns the composite state for the given cm handle id'
348 mockInventoryPersistence.getCmHandleState('some-cm-handle-id') >> compositeState
349 when: 'set data sync enabled is called with the data sync enabled flag set to #dataSyncEnabledFlag'
350 objectUnderTest.setDataSyncEnabled('some-cm-handle-id', dataSyncEnabledFlag)
351 then: 'the data sync enabled flag is set to #dataSyncEnabled'
352 compositeState.dataSyncEnabled == dataSyncEnabledFlag
353 and: 'the data store sync state is set to #expectedDataStoreSyncState'
354 compositeState.dataStores.operationalDataStore.dataStoreSyncState == expectedDataStoreSyncState
355 and: 'the cps data service to delete data nodes is invoked the expected number of times'
356 deleteDataNodeExpectedNumberOfInvocation * mockCpsDataService.deleteDataNode('NFP-Operational', 'some-cm-handle-id', '/netconf-state', _)
357 and: 'the inventory persistence service to update node leaves is called with the correct values'
358 saveCmHandleStateExpectedNumberOfInvocations * mockInventoryPersistence.saveCmHandleState('some-cm-handle-id', compositeState)
359 where: 'the following data sync enabled flag is used'
360 scenario | dataSyncEnabledFlag | initialDataSyncEnabledFlag | initialDataSyncState || expectedDataStoreSyncState | deleteDataNodeExpectedNumberOfInvocation | saveCmHandleStateExpectedNumberOfInvocations
361 'enabled' | true | false | DataStoreSyncState.NONE_REQUESTED || DataStoreSyncState.UNSYNCHRONIZED | 0 | 1
362 'disabled' | false | true | DataStoreSyncState.UNSYNCHRONIZED || DataStoreSyncState.NONE_REQUESTED | 0 | 1
363 'disabled where sync-state is currently SYNCHRONIZED' | false | true | DataStoreSyncState.SYNCHRONIZED || DataStoreSyncState.NONE_REQUESTED | 1 | 1
364 'is set to existing flag state' | true | true | DataStoreSyncState.UNSYNCHRONIZED || DataStoreSyncState.UNSYNCHRONIZED | 0 | 0
367 def 'Set cm Handle Data Sync Enabled flag with following cm handle not in ready state exception' () {
368 given: 'a cm handle composite state'
369 def compositeState = new CompositeState(cmHandleState: CmHandleState.ADVISED, dataSyncEnabled: false)
370 and: 'get cm handle state returns the composite state for the given cm handle id'
371 mockInventoryPersistence.getCmHandleState('some-cm-handle-id') >> compositeState
372 when: 'set data sync enabled is called with the data sync enabled flag set to true'
373 objectUnderTest.setDataSyncEnabled('some-cm-handle-id', true)
374 then: 'the expected exception is thrown'
376 and: 'the inventory persistence service to update node leaves is not invoked'
377 0 * mockInventoryPersistence.saveCmHandleState(_, _)
380 def 'Get all cm handle IDs by DMI plugin identifier.' () {
381 given: 'cm handle queries service returns cm handles'
382 1 * mockCmHandleQueries.getCmHandlesByDmiPluginIdentifier('some-dmi-plugin-identifier')
383 >> [new NcmpServiceCmHandle(cmHandleId: 'cm-handle-1'),
384 new NcmpServiceCmHandle(cmHandleId: 'cm-handle-2')]
385 when: 'cm handle Ids are requested with dmi plugin identifier'
386 def result = objectUnderTest.getAllCmHandleIdsByDmiPluginIdentifier('some-dmi-plugin-identifier')
387 then: 'the result size is correct'
388 assert result.size() == 2
389 and: 'the result returns the correct details'
390 assert result.containsAll('cm-handle-1','cm-handle-2')
394 CompositeState.DataStores.builder()
395 .operationalDataStore(CompositeState.Operational.builder()
396 .dataStoreSyncState(DataStoreSyncState.NONE_REQUESTED)
397 .lastSyncTime('some-timestamp').build()).build()