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