69654e341556a92cb456dd2d5c720cd289405ecc
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2021-2022 Bell Canada
6  *  Modifications Copyright (C) 2023 TechMahindra Ltd.
7  *  ================================================================================
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23
24 package org.onap.cps.ncmp.impl.inventory
25
26 import com.fasterxml.jackson.databind.ObjectMapper
27 import org.onap.cps.api.exceptions.DataValidationException
28 import org.onap.cps.api.model.ConditionProperties
29 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState
30 import org.onap.cps.ncmp.api.inventory.models.CmHandleQueryApiParameters
31 import org.onap.cps.ncmp.api.inventory.models.CmHandleQueryServiceParameters
32 import org.onap.cps.ncmp.api.inventory.models.CompositeState
33 import org.onap.cps.ncmp.api.inventory.models.ConditionApiProperties
34 import org.onap.cps.ncmp.api.inventory.models.DmiPluginRegistration
35 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle
36 import org.onap.cps.ncmp.api.inventory.models.TrustLevel
37 import org.onap.cps.ncmp.impl.NetworkCmProxyInventoryFacadeImpl
38 import org.onap.cps.ncmp.api.inventory.models.CmHandleState
39 import org.onap.cps.ncmp.api.inventory.models.LockReasonCategory
40 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle
41 import org.onap.cps.ncmp.impl.inventory.trustlevel.TrustLevelManager
42 import org.onap.cps.ncmp.impl.utils.AlternateIdMatcher
43 import org.onap.cps.utils.JsonObjectMapper
44 import reactor.core.publisher.Flux
45 import spock.lang.Specification
46
47 class NetworkCmProxyInventoryFacadeSpec extends Specification {
48
49     def mockCmHandleRegistrationService = Mock(CmHandleRegistrationService)
50     def mockCmHandleQueryService = Mock(CmHandleQueryService)
51     def mockParameterizedCmHandleQueryService = Mock(ParameterizedCmHandleQueryService)
52     def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
53     def mockInventoryPersistence = Mock(InventoryPersistence)
54     def mockTrustLevelManager = Mock(TrustLevelManager)
55     def mockAlternateIdMatcher = Mock(AlternateIdMatcher)
56     def objectUnderTest = new NetworkCmProxyInventoryFacadeImpl(mockCmHandleRegistrationService, mockCmHandleQueryService, mockParameterizedCmHandleQueryService, mockInventoryPersistence, spiedJsonObjectMapper, mockTrustLevelManager, mockAlternateIdMatcher)
57
58     def 'Update DMI Registration'() {
59         given: 'an (updated) dmi plugin registration'
60             def dmiPluginRegistration = Mock(DmiPluginRegistration)
61         when: 'the registration is submitted '
62            objectUnderTest.updateDmiRegistration(dmiPluginRegistration)
63         then: 'the call is delegated to the cm handle registration service'
64             1 * mockCmHandleRegistrationService.updateDmiRegistration(dmiPluginRegistration)
65     }
66
67     def 'Execute cm handle reference search for inventory'() {
68         given: 'a ConditionApiProperties object'
69             def conditionProperties = new ConditionProperties()
70             conditionProperties.conditionName = 'hasAllProperties'
71             conditionProperties.conditionParameters = [ [ 'some-key' : 'some-value' ] ]
72             def cmHandleQueryServiceParameters = new CmHandleQueryServiceParameters()
73             cmHandleQueryServiceParameters.cmHandleQueryParameters = [conditionProperties] as List<ConditionProperties>
74         and: 'the system returns an set of cmHandle ids'
75             mockParameterizedCmHandleQueryService.queryCmHandleIdsForInventory(*_) >> [ 'cmHandle1', 'cmHandle2' ]
76         when: 'executing the search'
77             def result = objectUnderTest.executeParameterizedCmHandleIdSearch(cmHandleQueryServiceParameters, false)
78         then: 'the result returns the correct 2 elements'
79             assert result.size() == 2
80             assert result.contains('cmHandle1')
81             assert result.contains('cmHandle2')
82     }
83
84     def 'Get all cm handle references by DMI plugin identifier and alternate id output option where #scenario.' () {
85         given: 'cm handle queries service returns cm handle references'
86             mockCmHandleQueryService.getCmHandleReferencesByDmiPluginIdentifier('some-dmi-plugin-identifier', false) >> ['cm-handle-1','cm-handle-2']
87             mockCmHandleQueryService.getCmHandleReferencesByDmiPluginIdentifier('some-dmi-plugin-identifier', true) >> ['alternate-id-1','alternate-id-2']
88         when: 'cm handle references are requested with dmi plugin identifier and alternate id output option'
89             def result = objectUnderTest.getAllCmHandleReferencesByDmiPluginIdentifier('some-dmi-plugin-identifier', outputAlternateId)
90         then: 'the result size is correct'
91             assert result.size() == 2
92         and: 'the result returns the correct details'
93             assert result.containsAll(expectedResult)
94         where:
95             scenario                        | outputAlternateId || expectedResult
96             'output is for alternate ids'   | true              || ['alternate-id-1', 'alternate-id-2']
97             'output is for cm handle ids'   | false             || ['cm-handle-1','cm-handle-2']
98     }
99
100     def 'Getting Yang Resources for a given #scenario'() {
101         when: 'yang resources is called'
102             objectUnderTest.getYangResourcesModuleReferences(cmHandleRef)
103         then: 'alternate id matcher is called'
104             mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> 'some-cm-handle'
105         and: 'CPS module services is invoked for the correct cm handle'
106             1 * mockInventoryPersistence.getYangResourcesModuleReferences('some-cm-handle')
107         where: 'following cm handle reference is used'
108             scenario                              | cmHandleRef
109             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
110             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
111     }
112
113     def 'Get a cm handle details using #scenario'() {
114         given: 'the system returns a yang modelled cm handle'
115             def dmiServiceName = 'some service name'
116             def compositeState = new CompositeState(cmHandleState: CmHandleState.ADVISED,
117                 lockReason: CompositeState.LockReason.builder().lockReasonCategory(LockReasonCategory.MODULE_SYNC_FAILED).details('lock details').build(),
118                 lastUpdateTime: 'some-timestamp',
119                 dataSyncEnabled: false,
120                 dataStores: dataStores())
121             def dmiProperties = [new YangModelCmHandle.Property('Book', 'Romance Novel')]
122             def publicProperties = [new YangModelCmHandle.Property('Public Book', 'Public Romance Novel')]
123             def moduleSetTag = 'some-module-set-tag'
124             def alternateId = 'some-alternate-id'
125             def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', dmiServiceName: dmiServiceName, dmiProperties: dmiProperties,
126                  publicProperties: publicProperties, compositeState: compositeState, moduleSetTag: moduleSetTag, alternateId: alternateId)
127             1 * mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> 'some-cm-handle'
128             1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
129             1 * mockTrustLevelManager.applyEffectiveTrustLevel(_) >> { args -> args[0].currentTrustLevel = TrustLevel.COMPLETE }
130         when: 'getting cm handle details for a given cm handle id from ncmp service'
131             def result = objectUnderTest.getNcmpServiceCmHandle(cmHandleRef)
132         then: 'the result is a ncmpServiceCmHandle'
133             assert result.class == NcmpServiceCmHandle.class
134         and: 'the cm handle contains the cm handle id'
135             assert result.cmHandleId == 'some-cm-handle'
136         and: 'the cm handle contains the alternate id'
137             assert result.alternateId == 'some-alternate-id'
138         and: 'the cm handle contains the module-set-tag'
139             assert result.moduleSetTag == 'some-module-set-tag'
140         and: 'the cm handle contains the DMI Properties'
141             assert result.dmiProperties ==[ Book:'Romance Novel' ]
142         and: 'the cm handle contains the public Properties'
143             assert result.publicProperties == [ "Public Book":'Public Romance Novel' ]
144         and: 'the cm handle contains the cm handle composite state'
145             assert result.compositeState == compositeState
146         and: 'the cm handle contains the trust level from the cache'
147             assert result.currentTrustLevel == TrustLevel.COMPLETE
148         where: 'following cm handle reference is used'
149             scenario                              | cmHandleRef
150             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
151             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
152     }
153
154     def 'Get cm handle public properties using #scenario'() {
155         given: 'a yang modelled cm handle'
156             def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
157             def publicProperties = [new YangModelCmHandle.Property('public prop', 'some public prop')]
158             def cmHandleId = 'some-cm-handle'
159             def alternateId = 'some-alternate-id'
160             def yangModelCmHandle = new YangModelCmHandle(id:cmHandleId, alternateId: alternateId, dmiServiceName: 'some service name', dmiProperties: dmiProperties, publicProperties: publicProperties)
161         and: 'we have corresponding cm handle for the cm handle reference'
162             1 * mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> cmHandleId
163         and: 'the system returns this yang modelled cm handle'
164             1 * mockInventoryPersistence.getYangModelCmHandle(cmHandleId) >> yangModelCmHandle
165         when: 'getting cm handle public properties for a given cm handle reference from ncmp service'
166             def result = objectUnderTest.getPublicCmHandleProperties(cmHandleRef)
167         then: 'the result returns the correct data'
168             assert result == [ 'public prop' : 'some public prop' ]
169         where: 'following cm handle reference is used'
170             scenario                              | cmHandleRef
171             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
172             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
173     }
174
175     def 'Get cm handle composite state using #scenario'() {
176         given: 'a yang modelled cm handle'
177             def compositeState = new CompositeState(cmHandleState: CmHandleState.ADVISED,
178                 lockReason: CompositeState.LockReason.builder().lockReasonCategory(LockReasonCategory.MODULE_SYNC_FAILED).details("lock details").build(),
179                 lastUpdateTime: 'some-timestamp',
180                 dataSyncEnabled: false,
181                 dataStores: dataStores())
182             def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
183             def publicProperties = [new YangModelCmHandle.Property('public prop', 'some public prop')]
184             def cmHandleId = 'some-cm-handle'
185             def alternateId = 'some-alternate-id'
186             def yangModelCmHandle = new YangModelCmHandle(id:cmHandleId, alternateId: alternateId, dmiServiceName: 'some service name', dmiProperties: dmiProperties, publicProperties: publicProperties, compositeState: compositeState)
187         and: 'we have corresponding cm handle for the cm handle reference'
188             1 * mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> cmHandleId
189         and: 'the system returns this yang modelled cm handle'
190             1 * mockInventoryPersistence.getYangModelCmHandle(cmHandleId) >> yangModelCmHandle
191         when: 'getting cm handle composite state for a given cm handle id from ncmp service'
192             def result = objectUnderTest.getCmHandleCompositeState(cmHandleRef)
193         then: 'the result returns the correct data'
194             assert result == compositeState
195         where: 'following cm handle reference is used'
196             scenario                              | cmHandleRef
197             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
198             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
199     }
200
201     def 'Execute cm handle reference search'() {
202         given: 'valid CmHandleQueryApiParameters input'
203             def cmHandleQueryApiParameters = new CmHandleQueryApiParameters()
204             def conditionApiProperties = new ConditionApiProperties()
205             conditionApiProperties.conditionName = 'hasAllModules'
206             conditionApiProperties.conditionParameters = [[moduleName: 'module-name-1']]
207             cmHandleQueryApiParameters.cmHandleQueryParameters = [conditionApiProperties]
208         and: 'query cm handle method return with a data node list'
209             mockParameterizedCmHandleQueryService.queryCmHandleReferenceIds(
210                 spiedJsonObjectMapper.convertToValueType(cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class), false)
211                 >> ['cm-handle-id-1']
212         when: 'execute cm handle search is called'
213             def result = objectUnderTest.executeCmHandleIdSearch(cmHandleQueryApiParameters, false)
214         then: 'result is the same collection as returned by the CPS Data Service'
215             assert result == ['cm-handle-id-1']
216     }
217
218     def 'Getting module definitions by module for a given #scenario'() {
219         when: 'get module definitions is performed with module name and cm handle reference'
220             objectUnderTest.getModuleDefinitionsByCmHandleAndModule(cmHandleRef, 'some-module', '2021-08-04')
221         then: 'alternate id matcher returns some cm handle id for a given cm handle reference'
222             mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> 'some-cm-handle'
223         and: 'ncmp inventory persistence service is invoked once with correct parameters'
224             1 * mockInventoryPersistence.getModuleDefinitionsByCmHandleAndModule('some-cm-handle', 'some-module', '2021-08-04')
225         where: 'following cm handle reference is used'
226             scenario                              | cmHandleRef
227             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
228             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
229     }
230
231     def 'Getting module definitions for a given #scenario'() {
232         when: 'get module definitions is performed with cm handle reference'
233             objectUnderTest.getModuleDefinitionsByCmHandleReference(cmHandleRef)
234         then: 'alternate id matcher returns some cm handle id for a given cm handle reference'
235             mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> 'some-cm-handle'
236         then: 'ncmp inventory persistence service is invoked once with correct parameter'
237             1 * mockInventoryPersistence.getModuleDefinitionsByCmHandleId('some-cm-handle')
238         where: 'following cm handle reference is used'
239             scenario                              | cmHandleRef
240             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
241             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
242     }
243
244     def 'Execute cm handle search'() {
245         given: 'valid CmHandleQueryApiParameters input'
246             def cmHandleQueryApiParameters = new CmHandleQueryApiParameters()
247             def conditionApiProperties = new ConditionApiProperties()
248             conditionApiProperties.conditionName = 'hasAllModules'
249             conditionApiProperties.conditionParameters = [[moduleName: 'module-name-1']]
250             cmHandleQueryApiParameters.cmHandleQueryParameters = [conditionApiProperties]
251         and: 'query cm handle method returns two cm handles'
252             mockParameterizedCmHandleQueryService.queryCmHandles(
253                 spiedJsonObjectMapper.convertToValueType(cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class))
254                 >> Flux.fromIterable([new NcmpServiceCmHandle(cmHandleId: 'ch-0', currentTrustLevel: TrustLevel.COMPLETE),
255                                       new NcmpServiceCmHandle(cmHandleId: 'ch-1', currentTrustLevel: TrustLevel.COMPLETE)])
256         when: 'execute cm handle search is called'
257             def result = objectUnderTest.executeCmHandleSearch(cmHandleQueryApiParameters).collectList().block()
258         then: 'result consists of the two cm handles returned by the CPS Data Service'
259             assert result.size() == 2
260             assert result[0].cmHandleId == 'ch-0'
261             assert result[1].cmHandleId == 'ch-1'
262         and: 'cm handles have trust level'
263             assert result[0].currentTrustLevel == TrustLevel.COMPLETE
264             assert result[1].currentTrustLevel == TrustLevel.COMPLETE
265     }
266
267     def 'Execute cm handle reference search with a valid condition name'() {
268         given: 'a valid API parameter with a supported condition'
269             def apiParams = new CmHandleQueryApiParameters(
270                     cmHandleQueryParameters: [
271                             new ConditionApiProperties(
272                                     conditionName: 'hasAllProperties',
273                                     conditionParameters: [[ 'some key': 'some value' ]]
274                             )
275                     ]
276             )
277         and: 'the system returns a cm handle id'
278             mockParameterizedCmHandleQueryService.queryInventoryForCmHandles(_)
279                     >> Flux.fromIterable([new NcmpServiceCmHandle(cmHandleId: 'cm handle from the query service')])
280         when: 'executing the cm handle search'
281             def result = objectUnderTest.executeCmHandleInventorySearch(apiParams).collectList().block()
282         then: 'the result returns the cm handle from the query service'
283             assert result.size() == 1
284             assert result[0].cmHandleId == 'cm handle from the query service'
285     }
286
287     def 'Execute cm handle reference search with an invalid condition name'() {
288         given: 'an API parameter with an unsupported condition name'
289             def apiParams = new CmHandleQueryApiParameters(
290                     cmHandleQueryParameters: [
291                             new ConditionApiProperties(conditionName: 'invalid condition name')
292                     ]
293             )
294         when: 'executing the search'
295             objectUnderTest.executeCmHandleInventorySearch(apiParams).collectList().block()
296         then: 'a data validation exception will be thrown'
297             def exception = thrown(DataValidationException)
298             assert exception.message == 'Invalid Query Parameter.'
299     }
300
301     def 'Set Cm Handle Data Sync flag.'() {
302         when: 'setting data sync enabled flag'
303             objectUnderTest.setDataSyncEnabled('ch-1',true)
304         then: 'call is delegated to the cm handle registration service'
305             mockCmHandleRegistrationService.setDataSyncEnabled('ch-1', true)
306     }
307
308     def dataStores() {
309         CompositeState.DataStores.builder().operationalDataStore(CompositeState.Operational.builder()
310                 .dataStoreSyncState(DataStoreSyncState.NONE_REQUESTED)
311                 .lastSyncTime('some-timestamp').build()).build()
312     }
313 }