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