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