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.impl.operations.YangModelCmHandleRetriever
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.models.DmiPluginRegistration
30 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
31 import org.onap.cps.spi.exceptions.DataValidationException
32 import org.onap.cps.ncmp.api.inventory.sync.ModuleSyncService
33 import spock.lang.Shared
35 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL
36 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING
37 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
38 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE
40 import org.onap.cps.utils.JsonObjectMapper
41 import com.fasterxml.jackson.databind.ObjectMapper
42 import org.onap.cps.api.CpsAdminService
43 import org.onap.cps.api.CpsDataService
44 import org.onap.cps.api.CpsModuleService
45 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
46 import org.onap.cps.spi.FetchDescendantsOption
47 import org.onap.cps.spi.model.DataNode
48 import org.springframework.http.HttpStatus
49 import org.springframework.http.ResponseEntity
50 import spock.lang.Specification
52 class NetworkCmProxyDataServiceImplSpec extends Specification {
54 def mockCpsDataService = Mock(CpsDataService)
55 def mockCpsModuleService = Mock(CpsModuleService)
56 def mockCpsAdminService = Mock(CpsAdminService)
57 def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
58 def mockDmiDataOperations = Mock(DmiDataOperations)
59 def nullNetworkCmProxyDataServicePropertyHandler = null
60 def mockYangModelCmHandleRetriever = Mock(YangModelCmHandleRetriever)
61 def mockModuleSyncService = Mock(ModuleSyncService)
62 def mockDmiPluginRegistration = Mock(DmiPluginRegistration)
65 def NO_REQUEST_ID = null
67 def OPTIONS_PARAM = '(a=1,b=2)'
69 def ncmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: 'some-cm-handle-id')
71 def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations,
72 mockCpsModuleService, mockCpsAdminService, nullNetworkCmProxyDataServicePropertyHandler, mockYangModelCmHandleRetriever, mockModuleSyncService)
74 def cmHandleXPath = "/dmi-registry/cm-handles[@id='testCmHandle']"
76 def dataNode = new DataNode(leaves: ['id': 'some-cm-handle', 'dmi-service-name': 'testDmiService'])
78 def 'Write resource data for pass-through running from DMI using POST.'() {
79 given: 'cpsDataService returns valid datanode'
80 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
81 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
82 when: 'write resource data is called'
83 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
84 'testResourceId', CREATE,
85 '{some-json}', 'application/json')
86 then: 'DMI called with correct data'
87 1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
88 CREATE, '{some-json}', 'application/json')
89 >> { new ResponseEntity<>(HttpStatus.CREATED) }
92 def 'Write resource data for pass-through running from DMI using an invalid id.'() {
93 when: 'write resource data is called'
94 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('invalid cm handle name',
95 'testResourceId', CREATE,
96 '{some-json}', 'application/json')
97 then: 'exception is thrown'
98 thrown(DataValidationException.class)
99 and: 'DMI is not invoked'
100 0 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(_, _, _, _, _)
103 def 'Get resource data for pass-through operational from DMI.'() {
104 given: 'get data node is called'
105 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
106 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
107 and: 'get resource data from DMI is called'
108 mockDmiDataOperations.getResourceDataFromDmi(
112 PASSTHROUGH_OPERATIONAL,
114 NO_TOPIC) >> new ResponseEntity<>('dmi-response', HttpStatus.OK)
115 when: 'get resource data operational for cm-handle is called'
116 def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
121 then: 'DMI returns a json response'
122 response == 'dmi-response'
125 def 'Get resource data for pass-through running from DMI.'() {
126 given: 'cpsDataService returns valid data node'
127 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
128 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
129 and: 'DMI returns valid response and data'
130 mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
135 NO_TOPIC) >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
136 when: 'get resource data is called'
137 def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
142 then: 'get resource data returns expected response'
143 response == '{dmi-response}'
146 def 'Getting Yang Resources.'() {
147 when: 'yang resources is called'
148 objectUnderTest.getYangResourcesModuleReferences('some-cm-handle')
149 then: 'CPS module services is invoked for the correct dataspace and cm handle'
150 1 * mockCpsModuleService.getYangResourcesModuleReferences('NFP-Operational','some-cm-handle')
153 def 'Getting Yang Resources with an invalid #scenario.'() {
154 when: 'yang resources is called'
155 objectUnderTest.getYangResourcesModuleReferences('invalid cm handle with spaces')
156 then: 'a data validation exception is thrown'
157 thrown(DataValidationException)
158 and: 'CPS module services is not invoked'
159 0 * mockCpsModuleService.getYangResourcesModuleReferences(*_)
162 def 'Get cm handle identifiers for the given module names.'() {
163 when: 'execute a cm handle search for the given module names'
164 objectUnderTest.executeCmHandleHasAllModulesSearch(['some-module-name'])
165 then: 'get anchor identifiers is invoked with the expected parameters'
166 1 * mockCpsAdminService.queryAnchorNames('NFP-Operational', ['some-module-name'])
169 def 'Get a cm handle.'() {
170 given: 'the system returns a yang modelled cm handle'
171 def dmiServiceName = 'some service name'
172 def dmiProperties = [new YangModelCmHandle.Property('Book', 'Romance Novel')]
173 def publicProperties = [new YangModelCmHandle.Property('Public Book', 'Public Romance Novel')]
174 def compositeState = new CompositeState(cmhandleState: 'ADVISED')
175 def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', dmiServiceName: dmiServiceName,
176 dmiProperties: dmiProperties, publicProperties: publicProperties, compositeState: compositeState)
177 1 * mockYangModelCmHandleRetriever.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
178 when: 'getting cm handle details for a given cm handle id from ncmp service'
179 def result = objectUnderTest.getNcmpServiceCmHandle('some-cm-handle')
180 then: 'the result returns the correct data'
181 result.cmHandleId == 'some-cm-handle'
182 result.dmiProperties ==[ Book:'Romance Novel' ]
183 result.publicProperties == [ "Public Book":'Public Romance Novel' ]
184 result.compositeState.cmhandleState == CmHandleState.ADVISED
187 def 'Get a cm handle with an invalid id.'() {
188 when: 'getting cm handle details for a given cm handle id with an invalid name'
189 objectUnderTest.getNcmpServiceCmHandle('invalid cm handle with spaces')
190 then: 'an exception is thrown'
191 thrown(DataValidationException)
192 and: 'the yang model cm handle retriever is not invoked'
193 0 * mockYangModelCmHandleRetriever.getYangModelCmHandle(*_)
196 def 'Get cm handle public properties'() {
197 given: 'a yang modelled cm handle'
198 def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
199 def publicProperties = [new YangModelCmHandle.Property('public prop', 'some public prop')]
200 def yangModelCmHandle = new YangModelCmHandle(id:'some-cm-handle', dmiServiceName: 'some service name', dmiProperties: dmiProperties, publicProperties: publicProperties)
201 and: 'the system returns this yang modelled cm handle'
202 1 * mockYangModelCmHandleRetriever.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
203 when: 'getting cm handle public properties for a given cm handle id from ncmp service'
204 def result = objectUnderTest.getCmHandlePublicProperties('some-cm-handle')
205 then: 'the result returns the correct data'
206 result == [ 'public prop' : 'some public prop' ]
209 def 'Get cm handle public properties with an invalid id.'() {
210 when: 'getting cm handle details for a given cm handle id with an invalid name'
211 objectUnderTest.getCmHandlePublicProperties('invalid cm handle with spaces')
212 then: 'an exception is thrown'
213 thrown(DataValidationException)
214 and: 'the yang model cm handle retriever is not invoked'
215 0 * mockYangModelCmHandleRetriever.getYangModelCmHandle(*_)
218 def 'Update resource data for pass-through running from dmi using POST #scenario DMI properties.'() {
219 given: 'cpsDataService returns valid datanode'
220 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
221 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
222 when: 'get resource data is called'
223 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
224 'testResourceId', UPDATE,
225 '{some-json}', 'application/json')
226 then: 'DMI called with correct data'
227 1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
228 UPDATE, '{some-json}', 'application/json')
229 >> { new ResponseEntity<>(HttpStatus.OK) }
232 def 'Verify modules and create anchor params'() {
233 given: 'dmi plugin registration return created cm handles'
234 def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'service1', dmiModelPlugin: 'service1',
235 dmiDataPlugin: 'service2')
236 dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
237 mockDmiPluginRegistration.getCreatedCmHandles() >> [ncmpServiceCmHandle]
238 when: 'parse and create cm handle in dmi registration then sync module'
239 objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(mockDmiPluginRegistration)
240 then: 'validate params for creating anchor and list elements'
241 1 * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
242 '/dmi-registry', '{"cm-handles":[{"id":"some-cm-handle-id",' +
243 '"additional-properties":[],"public-properties":[]}]}', null)
244 1 * mockCpsAdminService.createAnchor('NFP-Operational', null,