f704dbb3d69d9db03342d8c77ae6548e167c61ae
[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.api.impl
25
26 import com.fasterxml.jackson.databind.ObjectMapper
27 import org.onap.cps.ncmp.api.ParameterizedCmHandleQueryService
28 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueryService
29 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
30 import org.onap.cps.ncmp.api.impl.inventory.CompositeState
31 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState
32 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence
33 import org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory
34 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
35 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters
36 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters
37 import org.onap.cps.ncmp.api.models.ConditionApiProperties
38 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
39 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
40 import org.onap.cps.spi.model.ConditionProperties
41 import org.onap.cps.utils.JsonObjectMapper
42 import spock.lang.Specification
43
44 import java.util.stream.Collectors
45
46 class NetworkCmProxyInventoryFacadeSpec extends Specification {
47
48     def mockCmHandleRegistrationService = Mock(CmHandleRegistrationService)
49     def mockCmHandleQueryService = Mock(CmHandleQueryService)
50     def mockParameterizedCmHandleQueryService = Mock(ParameterizedCmHandleQueryService)
51     def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
52     def mockInventoryPersistence = Mock(InventoryPersistence)
53
54     def objectUnderTest = new NetworkCmProxyInventoryFacade(mockCmHandleRegistrationService, mockCmHandleQueryService, mockParameterizedCmHandleQueryService, mockInventoryPersistence, spiedJsonObjectMapper)
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: 'some-cm-handle', dmiServiceName: dmiServiceName,
113                 dmiProperties: dmiProperties, publicProperties: publicProperties, compositeState: compositeState,
114                 moduleSetTag: moduleSetTag, alternateId: alternateId)
115             1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
116         when: 'getting cm handle details for a given cm handle id from ncmp service'
117             def result = objectUnderTest.getNcmpServiceCmHandle('some-cm-handle')
118         then: 'the result is a ncmpServiceCmHandle'
119             result.class == NcmpServiceCmHandle.class
120         and: 'the cm handle contains the cm handle id'
121             result.cmHandleId == 'some-cm-handle'
122         and: 'the cm handle contains the alternate id'
123             result.alternateId == 'some-alternate-id'
124         and: 'the cm handle contains the module-set-tag'
125             result.moduleSetTag == 'some-module-set-tag'
126         and: 'the cm handle contains the DMI Properties'
127             result.dmiProperties ==[ Book:'Romance Novel' ]
128         and: 'the cm handle contains the public Properties'
129             result.publicProperties == [ "Public Book":'Public Romance Novel' ]
130         and: 'the cm handle contains the cm handle composite state'
131             result.compositeState == compositeState
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             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             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 return with a data node list'
204             mockParameterizedCmHandleQueryService.queryCmHandles(
205                 spiedJsonObjectMapper.convertToValueType(cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class))
206                 >> [new NcmpServiceCmHandle(cmHandleId: 'cm-handle-id-1')]
207         when: 'execute cm handle search is called'
208             def result = objectUnderTest.executeCmHandleSearch(cmHandleQueryApiParameters)
209         then: 'result is the same collection as returned by the CPS Data Service'
210             assert result.stream().map(cmHandle -> cmHandle.cmHandleId).collect(Collectors.toSet()) == ['cm-handle-id-1'] as Set
211     }
212
213     def 'Set Cm Handle Data Sync flag.'() {
214         when: 'setting data sync enabled flag'
215             objectUnderTest.setDataSyncEnabled('ch-1',true)
216         then: 'call is delegated to the cm handle registration service'
217             mockCmHandleRegistrationService.setDataSyncEnabled('ch-1', true)
218     }
219
220     def dataStores() {
221         CompositeState.DataStores.builder()
222             .operationalDataStore(CompositeState.Operational.builder()
223                 .dataStoreSyncState(DataStoreSyncState.NONE_REQUESTED)
224                 .lastSyncTime('some-timestamp').build()).build()
225     }
226 }