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