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 org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService
26 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
27 import org.onap.cps.ncmp.api.inventory.CmHandleState
28 import org.onap.cps.ncmp.api.inventory.CompositeState
29 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
30 import org.onap.cps.ncmp.api.inventory.LockReasonCategory
31 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState
32 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters
33 import org.onap.cps.ncmp.api.models.ConditionApiProperties
34 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
35 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
36 import org.onap.cps.spi.exceptions.DataValidationException
37 import org.onap.cps.spi.model.CmHandleQueryServiceParameters
38 import spock.lang.Shared
40 import java.util.stream.Collectors
42 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL
43 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING
44 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
45 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE
47 import org.onap.cps.utils.JsonObjectMapper
48 import com.fasterxml.jackson.databind.ObjectMapper
49 import org.onap.cps.api.CpsDataService
50 import org.onap.cps.api.CpsModuleService
51 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
52 import org.onap.cps.spi.FetchDescendantsOption
53 import org.onap.cps.spi.model.DataNode
54 import org.springframework.http.HttpStatus
55 import org.springframework.http.ResponseEntity
56 import spock.lang.Specification
58 class NetworkCmProxyDataServiceImplSpec extends Specification {
60 def mockCpsDataService = Mock(CpsDataService)
61 def mockCpsModuleService = Mock(CpsModuleService)
62 def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
63 def mockDmiDataOperations = Mock(DmiDataOperations)
64 def nullNetworkCmProxyDataServicePropertyHandler = null
65 def mockInventoryPersistence = Mock(InventoryPersistence)
66 def mockDmiPluginRegistration = Mock(DmiPluginRegistration)
67 def mockCpsCmHandlerQueryService = Mock(NetworkCmProxyCmHandlerQueryService)
70 def NO_REQUEST_ID = null
72 def OPTIONS_PARAM = '(a=1,b=2)'
74 def ncmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: 'some-cm-handle-id')
76 def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations,
77 mockCpsModuleService, nullNetworkCmProxyDataServicePropertyHandler, mockInventoryPersistence, mockCpsCmHandlerQueryService)
79 def cmHandleXPath = "/dmi-registry/cm-handles[@id='testCmHandle']"
81 def dataNode = new DataNode(leaves: ['id': 'some-cm-handle', 'dmi-service-name': 'testDmiService'])
83 def 'Write resource data for pass-through running from DMI using POST.'() {
84 given: 'cpsDataService returns valid datanode'
85 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
86 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
87 when: 'write resource data is called'
88 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
89 'testResourceId', CREATE,
90 '{some-json}', 'application/json')
91 then: 'DMI called with correct data'
92 1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
93 CREATE, '{some-json}', 'application/json')
94 >> { new ResponseEntity<>(HttpStatus.CREATED) }
97 def 'Write resource data for pass-through running from DMI using an invalid id.'() {
98 when: 'write resource data is called'
99 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('invalid cm handle name',
100 'testResourceId', CREATE,
101 '{some-json}', 'application/json')
102 then: 'exception is thrown'
103 thrown(DataValidationException.class)
104 and: 'DMI is not invoked'
105 0 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(_, _, _, _, _)
108 def 'Get resource data for pass-through operational from DMI.'() {
109 given: 'get data node is called'
110 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
111 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
112 and: 'get resource data from DMI is called'
113 mockDmiDataOperations.getResourceDataFromDmi(
117 PASSTHROUGH_OPERATIONAL,
119 NO_TOPIC) >> new ResponseEntity<>('dmi-response', HttpStatus.OK)
120 when: 'get resource data operational for cm-handle is called'
121 def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
126 then: 'DMI returns a json response'
127 response == 'dmi-response'
130 def 'Get resource data for pass-through running from DMI.'() {
131 given: 'cpsDataService returns valid data node'
132 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
133 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
134 and: 'DMI returns valid response and data'
135 mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
140 NO_TOPIC) >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
141 when: 'get resource data is called'
142 def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
147 then: 'get resource data returns expected response'
148 response == '{dmi-response}'
151 def 'Getting Yang Resources.'() {
152 when: 'yang resources is called'
153 objectUnderTest.getYangResourcesModuleReferences('some-cm-handle')
154 then: 'CPS module services is invoked for the correct dataspace and cm handle'
155 1 * mockCpsModuleService.getYangResourcesModuleReferences('NFP-Operational','some-cm-handle')
158 def 'Getting Yang Resources with an invalid #scenario.'() {
159 when: 'yang resources is called'
160 objectUnderTest.getYangResourcesModuleReferences('invalid cm handle with spaces')
161 then: 'a data validation exception is thrown'
162 thrown(DataValidationException)
163 and: 'CPS module services is not invoked'
164 0 * mockCpsModuleService.getYangResourcesModuleReferences(*_)
167 def 'Get a cm handle.'() {
168 given: 'the system returns a yang modelled cm handle'
169 def dmiServiceName = 'some service name'
170 def compositeState = new CompositeState(cmHandleState: CmHandleState.ADVISED,
171 lockReason: CompositeState.LockReason.builder().lockReasonCategory(LockReasonCategory.LOCKED_MISBEHAVING).details("lock misbehaving details").build(),
172 lastUpdateTime: 'some-timestamp',
173 dataSyncEnabled: false,
174 dataStores: dataStores())
175 def dmiProperties = [new YangModelCmHandle.Property('Book', 'Romance Novel')]
176 def publicProperties = [new YangModelCmHandle.Property('Public Book', 'Public Romance Novel')]
177 def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', dmiServiceName: dmiServiceName,
178 dmiProperties: dmiProperties, publicProperties: publicProperties, compositeState: compositeState)
179 1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
180 when: 'getting cm handle details for a given cm handle id from ncmp service'
181 def result = objectUnderTest.getNcmpServiceCmHandle('some-cm-handle')
182 then: 'the result is a ncmpServiceCmHandle'
183 result.class == NcmpServiceCmHandle.class
184 and: 'the cm handle contains the cm handle id'
185 result.cmHandleId == 'some-cm-handle'
186 and: 'the cm handle contains the DMI Properties'
187 result.dmiProperties ==[ Book:'Romance Novel' ]
188 and: 'the cm handle contains the public Properties'
189 result.publicProperties == [ "Public Book":'Public Romance Novel' ]
190 and: 'the cm handle contains the cm handle composite state'
191 result.compositeState == compositeState
195 def 'Get a cm handle with an invalid id.'() {
196 when: 'getting cm handle details for a given cm handle id with an invalid name'
197 objectUnderTest.getNcmpServiceCmHandle('invalid cm handle with spaces')
198 then: 'an exception is thrown'
199 thrown(DataValidationException)
200 and: 'the yang model cm handle retriever is not invoked'
201 0 * mockInventoryPersistence.getYangModelCmHandle(*_)
204 def 'Get cm handle public properties'() {
205 given: 'a yang modelled cm handle'
206 def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
207 def publicProperties = [new YangModelCmHandle.Property('public prop', 'some public prop')]
208 def yangModelCmHandle = new YangModelCmHandle(id:'some-cm-handle', dmiServiceName: 'some service name', dmiProperties: dmiProperties, publicProperties: publicProperties)
209 and: 'the system returns this yang modelled cm handle'
210 1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
211 when: 'getting cm handle public properties for a given cm handle id from ncmp service'
212 def result = objectUnderTest.getCmHandlePublicProperties('some-cm-handle')
213 then: 'the result returns the correct data'
214 result == [ 'public prop' : 'some public prop' ]
217 def 'Get cm handle public properties with an invalid id.'() {
218 when: 'getting cm handle public properties for a given cm handle id with an invalid name'
219 objectUnderTest.getCmHandlePublicProperties('invalid cm handle with spaces')
220 then: 'an exception is thrown'
221 thrown(DataValidationException)
222 and: 'the yang model cm handle retriever is not invoked'
223 0 * mockInventoryPersistence.getYangModelCmHandle(*_)
226 def 'Get cm handle composite state'() {
227 given: 'a yang modelled cm handle'
228 def compositeState = new CompositeState(cmHandleState: CmHandleState.ADVISED,
229 lockReason: CompositeState.LockReason.builder().lockReasonCategory(LockReasonCategory.LOCKED_MISBEHAVING).details("lock misbehaving details").build(),
230 lastUpdateTime: 'some-timestamp',
231 dataSyncEnabled: false,
232 dataStores: dataStores())
233 def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
234 def publicProperties = [new YangModelCmHandle.Property('public prop', 'some public prop')]
235 def yangModelCmHandle = new YangModelCmHandle(id:'some-cm-handle', dmiServiceName: 'some service name', dmiProperties: dmiProperties, publicProperties: publicProperties, compositeState: compositeState)
236 and: 'the system returns this yang modelled cm handle'
237 1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
238 when: 'getting cm handle composite state for a given cm handle id from ncmp service'
239 def result = objectUnderTest.getCmHandleCompositeState('some-cm-handle')
240 then: 'the result returns the correct data'
241 result == compositeState
244 def 'Get cm handle composite state with an invalid id.'() {
245 when: 'getting cm handle composite state for a given cm handle id with an invalid name'
246 objectUnderTest.getCmHandleCompositeState('invalid cm handle with spaces')
247 then: 'an exception is thrown'
248 thrown(DataValidationException)
249 and: 'the yang model cm handle retriever is not invoked'
250 0 * mockInventoryPersistence.getYangModelCmHandle(_)
253 def 'Update resource data for pass-through running from dmi using POST #scenario DMI properties.'() {
254 given: 'cpsDataService returns valid datanode'
255 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
256 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
257 when: 'get resource data is called'
258 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
259 'testResourceId', UPDATE,
260 '{some-json}', 'application/json')
261 then: 'DMI called with correct data'
262 1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
263 UPDATE, '{some-json}', 'application/json')
264 >> { new ResponseEntity<>(HttpStatus.OK) }
267 def 'Verify modules and create anchor params'() {
268 given: 'dmi plugin registration return created cm handles'
269 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'service1', dmiModelPlugin: 'service1',
270 dmiDataPlugin: 'service2')
271 dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
272 mockDmiPluginRegistration.getCreatedCmHandles() >> [ncmpServiceCmHandle]
273 when: 'parse and create cm handle in dmi registration then sync module'
274 objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(mockDmiPluginRegistration)
275 then: 'validate params for creating anchor and list elements'
276 1 * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry', '/dmi-registry', _, null) >> {
278 assert args[3].startsWith('{"cm-handles":[{"id":"some-cm-handle-id","state":{"cm-handle-state":"ADVISED","last-update-time":"20')
283 def 'Execute cm handle id search'() {
284 given: 'valid CmHandleQueryApiParameters input'
285 def cmHandleQueryApiParameters = new CmHandleQueryApiParameters()
286 def conditionApiProperties = new ConditionApiProperties()
287 conditionApiProperties.conditionName = 'hasAllModules'
288 conditionApiProperties.conditionParameters = [[moduleName: 'module-name-1']]
289 cmHandleQueryApiParameters.cmHandleQueryParameters = [conditionApiProperties]
290 and: 'query cm handle method return with a data node list'
291 mockCpsCmHandlerQueryService.queryCmHandleIds(
292 spiedJsonObjectMapper.convertToValueType(cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class))
293 >> ['cm-handle-id-1']
294 when: 'execute cm handle search is called'
295 def result = objectUnderTest.executeCmHandleIdSearch(cmHandleQueryApiParameters)
296 then: 'result is the same collection as returned by the CPS Data Service'
297 assert result == ['cm-handle-id-1'] as Set
300 def 'Getting module definitions.'() {
301 when: 'get module definitions method is called with a valid cm handle ID'
302 objectUnderTest.getModuleDefinitionsByCmHandleId('some-cm-handle')
303 then: 'CPS module services is invoked once'
304 1 * mockInventoryPersistence.getModuleDefinitionsByCmHandleId('some-cm-handle')
309 CompositeState.DataStores.builder()
310 .operationalDataStore(CompositeState.Operational.builder()
311 .dataStoreSyncState(DataStoreSyncState.NONE_REQUESTED)
312 .lastSyncTime('some-timestamp').build()).build()
315 def 'Execute cm handle search'() {
316 given: 'valid CmHandleQueryApiParameters input'
317 def cmHandleQueryApiParameters = new CmHandleQueryApiParameters()
318 def conditionApiProperties = new ConditionApiProperties()
319 conditionApiProperties.conditionName = 'hasAllModules'
320 conditionApiProperties.conditionParameters = [[moduleName: 'module-name-1']]
321 cmHandleQueryApiParameters.cmHandleQueryParameters = [conditionApiProperties]
322 and: 'query cm handle method return with a data node list'
323 mockCpsCmHandlerQueryService.queryCmHandles(
324 spiedJsonObjectMapper.convertToValueType(cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class))
325 >> [new NcmpServiceCmHandle(cmHandleId: 'cm-handle-id-1')]
326 when: 'execute cm handle search is called'
327 def result = objectUnderTest.executeCmHandleSearch(cmHandleQueryApiParameters)
328 then: 'result is the same collection as returned by the CPS Data Service'
329 assert result.stream().map(d -> d.cmHandleId).collect(Collectors.toSet()) == ['cm-handle-id-1'] as Set