282bd9e89bf34168769608be2af022c85e4b9ed7
[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             mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> 'some-cm-handle'
125             1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
126         and: 'a trust level for the cm handle in the cache'
127             mockTrustLevelManager.getEffectiveTrustLevel(*_) >> TrustLevel.COMPLETE
128         when: 'getting cm handle details for a given cm handle id from ncmp service'
129             def result = objectUnderTest.getNcmpServiceCmHandle(cmHandleRef)
130         then: 'the result is a ncmpServiceCmHandle'
131             assert result.class == NcmpServiceCmHandle.class
132         and: 'the cm handle contains the cm handle id'
133             assert result.cmHandleId == 'some-cm-handle'
134         and: 'the cm handle contains the alternate id'
135             assert result.alternateId == 'some-alternate-id'
136         and: 'the cm handle contains the module-set-tag'
137             assert result.moduleSetTag == 'some-module-set-tag'
138         and: 'the cm handle contains the DMI Properties'
139             assert result.dmiProperties ==[ Book:'Romance Novel' ]
140         and: 'the cm handle contains the public Properties'
141             assert result.publicProperties == [ "Public Book":'Public Romance Novel' ]
142         and: 'the cm handle contains the cm handle composite state'
143             assert result.compositeState == compositeState
144         and: 'the cm handle contains the trust level from the cache'
145             assert result.currentTrustLevel == TrustLevel.COMPLETE
146         where: 'following cm handle reference is used'
147             scenario                              | cmHandleRef
148             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
149             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
150     }
151
152     def 'Get cm handle public properties using #scenario'() {
153         given: 'a yang modelled cm handle'
154             def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
155             def publicProperties = [new YangModelCmHandle.Property('public prop', 'some public prop')]
156             def cmHandleId = 'some-cm-handle'
157             def alternateId = 'some-alternate-id'
158             def yangModelCmHandle = new YangModelCmHandle(id:cmHandleId, alternateId: alternateId, dmiServiceName: 'some service name', dmiProperties: dmiProperties, publicProperties: publicProperties)
159         and: 'we have corresponding cm handle for the cm handle reference'
160             1 * mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> cmHandleId
161         and: 'the system returns this yang modelled cm handle'
162             1 * mockInventoryPersistence.getYangModelCmHandle(cmHandleId) >> yangModelCmHandle
163         when: 'getting cm handle public properties for a given cm handle reference from ncmp service'
164             def result = objectUnderTest.getCmHandlePublicProperties(cmHandleRef)
165         then: 'the result returns the correct data'
166             assert result == [ 'public prop' : 'some public prop' ]
167         where: 'following cm handle reference is used'
168             scenario                              | cmHandleRef
169             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
170             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
171     }
172
173     def 'Get cm handle composite state using #scenario'() {
174         given: 'a yang modelled cm handle'
175             def compositeState = new CompositeState(cmHandleState: CmHandleState.ADVISED,
176                 lockReason: CompositeState.LockReason.builder().lockReasonCategory(LockReasonCategory.MODULE_SYNC_FAILED).details("lock details").build(),
177                 lastUpdateTime: 'some-timestamp',
178                 dataSyncEnabled: false,
179                 dataStores: dataStores())
180             def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
181             def publicProperties = [new YangModelCmHandle.Property('public prop', 'some public prop')]
182             def cmHandleId = 'some-cm-handle'
183             def alternateId = 'some-alternate-id'
184             def yangModelCmHandle = new YangModelCmHandle(id:cmHandleId, alternateId: alternateId, dmiServiceName: 'some service name', dmiProperties: dmiProperties, publicProperties: publicProperties, compositeState: compositeState)
185         and: 'we have corresponding cm handle for the cm handle reference'
186             1 * mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> cmHandleId
187         and: 'the system returns this yang modelled cm handle'
188             1 * mockInventoryPersistence.getYangModelCmHandle(cmHandleId) >> yangModelCmHandle
189         when: 'getting cm handle composite state for a given cm handle id from ncmp service'
190             def result = objectUnderTest.getCmHandleCompositeState(cmHandleRef)
191         then: 'the result returns the correct data'
192             assert result == compositeState
193         where: 'following cm handle reference is used'
194             scenario                              | cmHandleRef
195             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
196             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
197     }
198
199     def 'Execute cm handle reference search'() {
200         given: 'valid CmHandleQueryApiParameters input'
201             def cmHandleQueryApiParameters = new CmHandleQueryApiParameters()
202             def conditionApiProperties = new ConditionApiProperties()
203             conditionApiProperties.conditionName = 'hasAllModules'
204             conditionApiProperties.conditionParameters = [[moduleName: 'module-name-1']]
205             cmHandleQueryApiParameters.cmHandleQueryParameters = [conditionApiProperties]
206         and: 'query cm handle method return with a data node list'
207             mockParameterizedCmHandleQueryService.queryCmHandleReferenceIds(
208                 spiedJsonObjectMapper.convertToValueType(cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class), false)
209                 >> ['cm-handle-id-1']
210         when: 'execute cm handle search is called'
211             def result = objectUnderTest.executeCmHandleIdSearch(cmHandleQueryApiParameters, false)
212         then: 'result is the same collection as returned by the CPS Data Service'
213             assert result == ['cm-handle-id-1']
214     }
215
216     def 'Getting module definitions by module for a given #scenario'() {
217         when: 'get module definitions is performed with module name and cm handle reference'
218             objectUnderTest.getModuleDefinitionsByCmHandleAndModule(cmHandleRef, 'some-module', '2021-08-04')
219         then: 'alternate id matcher returns some cm handle id for a given cm handle reference'
220             mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> 'some-cm-handle'
221         and: 'ncmp inventory persistence service is invoked once with correct parameters'
222             1 * mockInventoryPersistence.getModuleDefinitionsByCmHandleAndModule('some-cm-handle', 'some-module', '2021-08-04')
223         where: 'following cm handle reference is used'
224             scenario                              | cmHandleRef
225             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
226             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
227     }
228
229     def 'Getting module definitions for a given #scenario'() {
230         when: 'get module definitions is performed with cm handle reference'
231             objectUnderTest.getModuleDefinitionsByCmHandleReference(cmHandleRef)
232         then: 'alternate id matcher returns some cm handle id for a given cm handle reference'
233             mockAlternateIdMatcher.getCmHandleId(cmHandleRef) >> 'some-cm-handle'
234         then: 'ncmp inventory persistence service is invoked once with correct parameter'
235             1 * mockInventoryPersistence.getModuleDefinitionsByCmHandleId('some-cm-handle')
236         where: 'following cm handle reference is used'
237             scenario                              | cmHandleRef
238             'Cm Handle Reference as cm handle-id' | 'some-cm-handle'
239             'Cm Handle Reference as alternate-id' | 'some-alternate-id'
240     }
241
242     def 'Execute cm handle search'() {
243         given: 'valid CmHandleQueryApiParameters input'
244             def cmHandleQueryApiParameters = new CmHandleQueryApiParameters()
245             def conditionApiProperties = new ConditionApiProperties()
246             conditionApiProperties.conditionName = 'hasAllModules'
247             conditionApiProperties.conditionParameters = [[moduleName: 'module-name-1']]
248             cmHandleQueryApiParameters.cmHandleQueryParameters = [conditionApiProperties]
249         and: 'query cm handle method returns two cm handles'
250             mockParameterizedCmHandleQueryService.queryCmHandles(
251                 spiedJsonObjectMapper.convertToValueType(cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class))
252                 >> [new NcmpServiceCmHandle(cmHandleId: 'ch-0'), new NcmpServiceCmHandle(cmHandleId: 'ch-1')]
253         and: 'a trust level for cm handles'
254             mockTrustLevelManager.getEffectiveTrustLevel(*_) >> TrustLevel.COMPLETE
255         when: 'execute cm handle search is called'
256             def result = objectUnderTest.executeCmHandleSearch(cmHandleQueryApiParameters)
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 }