29cd92db3f330580a00ae09e163a1b0cf06fa0d1
[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.model.ConditionProperties
28 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState
29 import org.onap.cps.ncmp.api.inventory.models.CmHandleQueryApiParameters
30 import org.onap.cps.ncmp.api.inventory.models.CmHandleQueryServiceParameters
31 import org.onap.cps.ncmp.api.inventory.models.CompositeState
32 import org.onap.cps.ncmp.api.inventory.models.ConditionApiProperties
33 import org.onap.cps.ncmp.api.inventory.models.DmiPluginRegistration
34 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle
35 import org.onap.cps.ncmp.api.inventory.models.TrustLevel
36 import org.onap.cps.ncmp.impl.NetworkCmProxyInventoryFacadeImpl
37 import org.onap.cps.ncmp.api.inventory.models.CmHandleState
38 import org.onap.cps.ncmp.api.inventory.models.LockReasonCategory
39 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle
40 import org.onap.cps.ncmp.impl.inventory.trustlevel.TrustLevelManager
41 import org.onap.cps.ncmp.impl.utils.AlternateIdMatcher
42 import org.onap.cps.utils.JsonObjectMapper
43 import reactor.core.publisher.Flux
44 import spock.lang.Specification
45
46 class NetworkCmProxyInventoryFacadeSpec extends Specification {
47
48     def mockCmHandleRegistrationService = Mock(CmHandleRegistrationService)
49     def mockCmHandleQueryService = Mock(CmHandleQueryService)
50     def mockParameterizedCmHandleQueryService = Mock(ParameterizedCmHandleQueryService)
51     def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
52     def mockInventoryPersistence = Mock(InventoryPersistence)
53     def mockTrustLevelManager = Mock(TrustLevelManager)
54     def mockAlternateIdMatcher = Mock(AlternateIdMatcher)
55     def objectUnderTest = new NetworkCmProxyInventoryFacadeImpl(mockCmHandleRegistrationService, mockCmHandleQueryService, mockParameterizedCmHandleQueryService, mockInventoryPersistence, spiedJsonObjectMapper, mockTrustLevelManager, mockAlternateIdMatcher)
56
57     def 'Update DMI Registration'() {
58         given: 'an (updated) dmi plugin registration'
59             def dmiPluginRegistration = Mock(DmiPluginRegistration)
60         when: 'the registration is submitted '
61            objectUnderTest.updateDmiRegistration(dmiPluginRegistration)
62         then: 'the call is delegated to the cm handle registration service'
63             1 * mockCmHandleRegistrationService.updateDmiRegistration(dmiPluginRegistration)
64     }
65
66     def 'Execute cm handle reference search for inventory'() {
67         given: 'a ConditionApiProperties object'
68             def conditionProperties = new ConditionProperties()
69             conditionProperties.conditionName = 'hasAllProperties'
70             conditionProperties.conditionParameters = [ [ 'some-key' : 'some-value' ] ]
71             def cmHandleQueryServiceParameters = new CmHandleQueryServiceParameters()
72             cmHandleQueryServiceParameters.cmHandleQueryParameters = [conditionProperties] as List<ConditionProperties>
73         and: 'the system returns an set of cmHandle ids'
74             mockParameterizedCmHandleQueryService.queryCmHandleIdsForInventory(*_) >> [ 'cmHandle1', 'cmHandle2' ]
75         when: 'executing the search'
76             def result = objectUnderTest.executeParameterizedCmHandleIdSearch(cmHandleQueryServiceParameters, false)
77         then: 'the result returns the correct 2 elements'
78             assert result.size() == 2
79             assert result.contains('cmHandle1')
80             assert result.contains('cmHandle2')
81     }
82
83     def 'Get all cm handle references by DMI plugin identifier and alternate id output option where #scenario.' () {
84         given: 'cm handle queries service returns cm handle references'
85             mockCmHandleQueryService.getCmHandleReferencesByDmiPluginIdentifier('some-dmi-plugin-identifier', false) >> ['cm-handle-1','cm-handle-2']
86             mockCmHandleQueryService.getCmHandleReferencesByDmiPluginIdentifier('some-dmi-plugin-identifier', true) >> ['alternate-id-1','alternate-id-2']
87         when: 'cm handle references are requested with dmi plugin identifier and alternate id output option'
88             def result = objectUnderTest.getAllCmHandleReferencesByDmiPluginIdentifier('some-dmi-plugin-identifier', outputAlternateId)
89         then: 'the result size is correct'
90             assert result.size() == 2
91         and: 'the result returns the correct details'
92             assert result.containsAll(expectedResult)
93         where:
94             scenario                        | outputAlternateId || expectedResult
95             'output is for alternate ids'   | true              || ['alternate-id-1', 'alternate-id-2']
96             'output is for cm handle ids'   | false             || ['cm-handle-1','cm-handle-2']
97     }
98
99     def 'Getting Yang Resources for a given #scenario'() {
100         when: 'yang resources is called'
101             objectUnderTest.getYangResourcesModuleReferences(cmHandleRef)
102         then: 'alternate id matcher is called'
103             mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> 'some-cm-handle'
104         and: 'CPS module services is invoked for the correct cm handle'
105             1 * mockInventoryPersistence.getYangResourcesModuleReferences('some-cm-handle')
106         where: 'following cm handle reference is used'
107             scenario                              | cmHandleRef
108             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
109             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
110     }
111
112     def 'Get a cm handle details using #scenario'() {
113         given: 'the system returns a yang modelled cm handle'
114             def dmiServiceName = 'some service name'
115             def compositeState = new CompositeState(cmHandleState: CmHandleState.ADVISED,
116                 lockReason: CompositeState.LockReason.builder().lockReasonCategory(LockReasonCategory.MODULE_SYNC_FAILED).details('lock details').build(),
117                 lastUpdateTime: 'some-timestamp',
118                 dataSyncEnabled: false,
119                 dataStores: dataStores())
120             def dmiProperties = [new YangModelCmHandle.Property('Book', 'Romance Novel')]
121             def publicProperties = [new YangModelCmHandle.Property('Public Book', 'Public Romance Novel')]
122             def moduleSetTag = 'some-module-set-tag'
123             def alternateId = 'some-alternate-id'
124             def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', dmiServiceName: dmiServiceName, dmiProperties: dmiProperties,
125                  publicProperties: publicProperties, compositeState: compositeState, moduleSetTag: moduleSetTag, alternateId: alternateId)
126             1 * mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> 'some-cm-handle'
127             1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
128             1 * mockTrustLevelManager.applyEffectiveTrustLevel(_) >> { args -> args[0].currentTrustLevel = TrustLevel.COMPLETE }
129         when: 'getting cm handle details for a given cm handle id from ncmp service'
130             def result = objectUnderTest.getNcmpServiceCmHandle(cmHandleRef)
131         then: 'the result is a ncmpServiceCmHandle'
132             assert result.class == NcmpServiceCmHandle.class
133         and: 'the cm handle contains the cm handle id'
134             assert result.cmHandleId == 'some-cm-handle'
135         and: 'the cm handle contains the alternate id'
136             assert result.alternateId == 'some-alternate-id'
137         and: 'the cm handle contains the module-set-tag'
138             assert result.moduleSetTag == 'some-module-set-tag'
139         and: 'the cm handle contains the DMI Properties'
140             assert result.dmiProperties ==[ Book:'Romance Novel' ]
141         and: 'the cm handle contains the public Properties'
142             assert result.publicProperties == [ "Public Book":'Public Romance Novel' ]
143         and: 'the cm handle contains the cm handle composite state'
144             assert result.compositeState == compositeState
145         and: 'the cm handle contains the trust level from the cache'
146             assert result.currentTrustLevel == TrustLevel.COMPLETE
147         where: 'following cm handle reference is used'
148             scenario                              | cmHandleRef
149             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
150             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
151     }
152
153     def 'Get cm handle public properties using #scenario'() {
154         given: 'a yang modelled cm handle'
155             def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
156             def publicProperties = [new YangModelCmHandle.Property('public prop', 'some public prop')]
157             def cmHandleId = 'some-cm-handle'
158             def alternateId = 'some-alternate-id'
159             def yangModelCmHandle = new YangModelCmHandle(id:cmHandleId, alternateId: alternateId, dmiServiceName: 'some service name', dmiProperties: dmiProperties, publicProperties: publicProperties)
160         and: 'we have corresponding cm handle for the cm handle reference'
161             1 * mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> cmHandleId
162         and: 'the system returns this yang modelled cm handle'
163             1 * mockInventoryPersistence.getYangModelCmHandle(cmHandleId) >> yangModelCmHandle
164         when: 'getting cm handle public properties for a given cm handle reference from ncmp service'
165             def result = objectUnderTest.getCmHandlePublicProperties(cmHandleRef)
166         then: 'the result returns the correct data'
167             assert result == [ 'public prop' : 'some public prop' ]
168         where: 'following cm handle reference is used'
169             scenario                              | cmHandleRef
170             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
171             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
172     }
173
174     def 'Get cm handle composite state using #scenario'() {
175         given: 'a yang modelled cm handle'
176             def compositeState = new CompositeState(cmHandleState: CmHandleState.ADVISED,
177                 lockReason: CompositeState.LockReason.builder().lockReasonCategory(LockReasonCategory.MODULE_SYNC_FAILED).details("lock details").build(),
178                 lastUpdateTime: 'some-timestamp',
179                 dataSyncEnabled: false,
180                 dataStores: dataStores())
181             def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
182             def publicProperties = [new YangModelCmHandle.Property('public prop', 'some public prop')]
183             def cmHandleId = 'some-cm-handle'
184             def alternateId = 'some-alternate-id'
185             def yangModelCmHandle = new YangModelCmHandle(id:cmHandleId, alternateId: alternateId, dmiServiceName: 'some service name', dmiProperties: dmiProperties, publicProperties: publicProperties, compositeState: compositeState)
186         and: 'we have corresponding cm handle for the cm handle reference'
187             1 * mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> cmHandleId
188         and: 'the system returns this yang modelled cm handle'
189             1 * mockInventoryPersistence.getYangModelCmHandle(cmHandleId) >> yangModelCmHandle
190         when: 'getting cm handle composite state for a given cm handle id from ncmp service'
191             def result = objectUnderTest.getCmHandleCompositeState(cmHandleRef)
192         then: 'the result returns the correct data'
193             assert result == compositeState
194         where: 'following cm handle reference is used'
195             scenario                              | cmHandleRef
196             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
197             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
198     }
199
200     def 'Execute cm handle reference search'() {
201         given: 'valid CmHandleQueryApiParameters input'
202             def cmHandleQueryApiParameters = new CmHandleQueryApiParameters()
203             def conditionApiProperties = new ConditionApiProperties()
204             conditionApiProperties.conditionName = 'hasAllModules'
205             conditionApiProperties.conditionParameters = [[moduleName: 'module-name-1']]
206             cmHandleQueryApiParameters.cmHandleQueryParameters = [conditionApiProperties]
207         and: 'query cm handle method return with a data node list'
208             mockParameterizedCmHandleQueryService.queryCmHandleReferenceIds(
209                 spiedJsonObjectMapper.convertToValueType(cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class), false)
210                 >> ['cm-handle-id-1']
211         when: 'execute cm handle search is called'
212             def result = objectUnderTest.executeCmHandleIdSearch(cmHandleQueryApiParameters, false)
213         then: 'result is the same collection as returned by the CPS Data Service'
214             assert result == ['cm-handle-id-1']
215     }
216
217     def 'Getting module definitions by module for a given #scenario'() {
218         when: 'get module definitions is performed with module name and cm handle reference'
219             objectUnderTest.getModuleDefinitionsByCmHandleAndModule(cmHandleRef, 'some-module', '2021-08-04')
220         then: 'alternate id matcher returns some cm handle id for a given cm handle reference'
221             mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> 'some-cm-handle'
222         and: 'ncmp inventory persistence service is invoked once with correct parameters'
223             1 * mockInventoryPersistence.getModuleDefinitionsByCmHandleAndModule('some-cm-handle', 'some-module', '2021-08-04')
224         where: 'following cm handle reference is used'
225             scenario                              | cmHandleRef
226             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
227             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
228     }
229
230     def 'Getting module definitions for a given #scenario'() {
231         when: 'get module definitions is performed with cm handle reference'
232             objectUnderTest.getModuleDefinitionsByCmHandleReference(cmHandleRef)
233         then: 'alternate id matcher returns some cm handle id for a given cm handle reference'
234             mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> 'some-cm-handle'
235         then: 'ncmp inventory persistence service is invoked once with correct parameter'
236             1 * mockInventoryPersistence.getModuleDefinitionsByCmHandleId('some-cm-handle')
237         where: 'following cm handle reference is used'
238             scenario                              | cmHandleRef
239             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
240             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
241     }
242
243     def 'Execute cm handle search'() {
244         given: 'valid CmHandleQueryApiParameters input'
245             def cmHandleQueryApiParameters = new CmHandleQueryApiParameters()
246             def conditionApiProperties = new ConditionApiProperties()
247             conditionApiProperties.conditionName = 'hasAllModules'
248             conditionApiProperties.conditionParameters = [[moduleName: 'module-name-1']]
249             cmHandleQueryApiParameters.cmHandleQueryParameters = [conditionApiProperties]
250         and: 'query cm handle method returns two cm handles'
251             mockParameterizedCmHandleQueryService.queryCmHandles(
252                 spiedJsonObjectMapper.convertToValueType(cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class))
253                 >> Flux.fromIterable([new NcmpServiceCmHandle(cmHandleId: 'ch-0', currentTrustLevel: TrustLevel.COMPLETE),
254                                       new NcmpServiceCmHandle(cmHandleId: 'ch-1', currentTrustLevel: TrustLevel.COMPLETE)])
255         when: 'execute cm handle search is called'
256             def result = objectUnderTest.executeCmHandleSearch(cmHandleQueryApiParameters).collectList().block()
257         then: 'result consists of the two cm handles returned by the CPS Data Service'
258             assert result.size() == 2
259             assert result[0].cmHandleId == 'ch-0'
260             assert result[1].cmHandleId == 'ch-1'
261         and: 'cm handles have trust level'
262             assert result[0].currentTrustLevel == TrustLevel.COMPLETE
263             assert result[1].currentTrustLevel == TrustLevel.COMPLETE
264     }
265
266     def 'Set Cm Handle Data Sync flag.'() {
267         when: 'setting data sync enabled flag'
268             objectUnderTest.setDataSyncEnabled('ch-1',true)
269         then: 'call is delegated to the cm handle registration service'
270             mockCmHandleRegistrationService.setDataSyncEnabled('ch-1', true)
271     }
272
273     def dataStores() {
274         CompositeState.DataStores.builder().operationalDataStore(CompositeState.Operational.builder()
275                 .dataStoreSyncState(DataStoreSyncState.NONE_REQUESTED)
276                 .lastSyncTime('some-timestamp').build()).build()
277     }
278 }