19b6199b997a42fa2d8a0b6e889fd923a58f9fb0
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2025 OpenInfra Foundation Europe. All rights reserved.
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.impl.inventory;
22
23 import static org.onap.cps.ncmp.impl.inventory.CmHandleQueryParametersValidator.validateCpsPathConditionProperties;
24 import static org.onap.cps.ncmp.impl.inventory.CmHandleQueryParametersValidator.validateModuleNameConditionProperties;
25 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.HAS_ALL_MODULES;
26 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.HAS_ALL_PROPERTIES;
27 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.WITH_CPS_PATH;
28 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.WITH_TRUST_LEVEL;
29
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.function.BiFunction;
37 import lombok.RequiredArgsConstructor;
38 import org.onap.cps.api.exceptions.DataValidationException;
39 import org.onap.cps.api.model.ConditionProperties;
40 import org.onap.cps.cpspath.parser.PathParsingException;
41 import org.onap.cps.ncmp.api.inventory.models.CmHandleQueryServiceParameters;
42 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle;
43 import org.onap.cps.ncmp.impl.inventory.models.InventoryQueryConditions;
44 import org.onap.cps.ncmp.impl.inventory.models.PropertyType;
45 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle;
46 import org.onap.cps.ncmp.impl.inventory.trustlevel.TrustLevelManager;
47 import org.onap.cps.ncmp.impl.utils.YangDataConverter;
48 import org.springframework.stereotype.Service;
49 import reactor.core.publisher.Flux;
50
51 @Service
52 @RequiredArgsConstructor
53 public class ParameterizedCmHandleQueryServiceImpl implements ParameterizedCmHandleQueryService {
54
55     private static final int FLUX_BUFFER_SIZE = 1000;
56     private static final Collection<String> NO_QUERY_TO_EXECUTE = null;
57     private final CmHandleQueryService cmHandleQueryService;
58     private final InventoryPersistence inventoryPersistence;
59     private final TrustLevelManager trustLevelManager;
60
61     @Override
62     public Collection<String> queryCmHandleReferenceIds(
63             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
64             final boolean outputAlternateId) {
65         return executeQueries(cmHandleQueryServiceParameters, outputAlternateId,
66                 this::executeCpsPathQuery,
67                 this::queryCmHandlesByPublicProperties,
68                 this::executeModuleNameQuery,
69                 this::queryCmHandlesByTrustLevel);
70     }
71
72     @Override
73     public Collection<String> queryCmHandleIdsForInventory(
74             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
75             final boolean outputAlternateId) {
76         return executeQueries(cmHandleQueryServiceParameters, outputAlternateId,
77                 this::executeCpsPathQuery,
78                 this::queryCmHandlesByPublicProperties,
79                 this::queryCmHandlesByPrivateProperties,
80                 this::queryCmHandlesByDmiPlugin);
81     }
82
83     @Override
84     public Flux<NcmpServiceCmHandle> queryCmHandles(final CmHandleQueryServiceParameters queryParameters) {
85         final Collection<String> cmHandleIds = queryCmHandleReferenceIds(queryParameters, false);
86         return getNcmpServiceCmHandles(cmHandleIds);
87     }
88
89     private Collection<String> queryCmHandlesByDmiPlugin(
90             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final boolean outputAlternateId) {
91         final Map<String, String> dmiPropertyQueryPairs =
92                 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
93                         InventoryQueryConditions.CM_HANDLE_WITH_DMI_PLUGIN.getName());
94         if (dmiPropertyQueryPairs.isEmpty()) {
95             return NO_QUERY_TO_EXECUTE;
96         }
97
98         final String dmiPluginIdentifierValue = dmiPropertyQueryPairs
99                 .get(PropertyType.DMI_PLUGIN.getYangContainerName());
100
101         return cmHandleQueryService.getCmHandleReferencesByDmiPluginIdentifier(
102                 dmiPluginIdentifierValue, outputAlternateId);
103
104     }
105
106     private Collection<String> queryCmHandlesByPrivateProperties(
107             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final boolean outputAlternateId) {
108
109         final Map<String, String> privatePropertyQueryPairs =
110                 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
111                         InventoryQueryConditions.HAS_ALL_ADDITIONAL_PROPERTIES.getName());
112
113         if (privatePropertyQueryPairs.isEmpty()) {
114             return NO_QUERY_TO_EXECUTE;
115         }
116         return cmHandleQueryService.queryCmHandleAdditionalProperties(privatePropertyQueryPairs, outputAlternateId);
117     }
118
119     private Collection<String> queryCmHandlesByPublicProperties(
120             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final boolean outputAlternateId) {
121
122         final Map<String, String> publicPropertyQueryPairs =
123                 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
124                         HAS_ALL_PROPERTIES.getConditionName());
125
126         if (publicPropertyQueryPairs.isEmpty()) {
127             return NO_QUERY_TO_EXECUTE;
128         }
129         return cmHandleQueryService.queryCmHandlePublicProperties(publicPropertyQueryPairs, outputAlternateId);
130     }
131
132     private Collection<String> queryCmHandlesByTrustLevel(final CmHandleQueryServiceParameters
133                                                                   cmHandleQueryServiceParameters,
134                                                           final boolean outputAlternateId) {
135
136         final Map<String, String> trustLevelPropertyQueryPairs =
137                 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
138                         WITH_TRUST_LEVEL.getConditionName());
139
140         if (trustLevelPropertyQueryPairs.isEmpty()) {
141             return NO_QUERY_TO_EXECUTE;
142         }
143         return cmHandleQueryService.queryCmHandlesByTrustLevel(trustLevelPropertyQueryPairs, outputAlternateId);
144     }
145
146     private Collection<String> executeModuleNameQuery(
147             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final boolean outputAlternateId) {
148         final Collection<String> moduleNamesForQuery =
149                 getModuleNamesForQuery(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
150         if (moduleNamesForQuery.isEmpty()) {
151             return NO_QUERY_TO_EXECUTE;
152         }
153         return inventoryPersistence.getCmHandleReferencesWithGivenModules(moduleNamesForQuery, outputAlternateId);
154     }
155
156     private Collection<String> executeCpsPathQuery(
157             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final boolean outputAlternateId) {
158         final Map<String, String> cpsPathCondition
159                 = getCpsPathCondition(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
160         if (!validateCpsPathConditionProperties(cpsPathCondition)) {
161             return Collections.emptySet();
162         }
163         final Collection<String> cpsPathQueryResult;
164         if (cpsPathCondition.isEmpty()) {
165             return NO_QUERY_TO_EXECUTE;
166         }
167         try {
168             cpsPathQueryResult = cmHandleQueryService.getCmHandleReferencesByCpsPath(cpsPathCondition.get("cpsPath"),
169                     outputAlternateId);
170         } catch (final PathParsingException pathParsingException) {
171             throw new DataValidationException(pathParsingException.getMessage(), pathParsingException.getDetails(),
172                     pathParsingException);
173         }
174         return cpsPathQueryResult;
175     }
176
177     private Collection<String> getModuleNamesForQuery(final List<ConditionProperties> conditionProperties) {
178         final List<String> result = new ArrayList<>();
179         getConditions(conditionProperties, HAS_ALL_MODULES.getConditionName()).forEach(
180                 conditionProperty -> {
181                     validateModuleNameConditionProperties(conditionProperty);
182                     result.add(conditionProperty.get("moduleName"));
183                 });
184         return result;
185     }
186
187     private Map<String, String> getCpsPathCondition(final List<ConditionProperties> conditionProperties) {
188         final Map<String, String> result = new HashMap<>();
189         getConditions(conditionProperties, WITH_CPS_PATH.getConditionName()).forEach(result::putAll);
190         return result;
191     }
192
193     private Map<String, String> getPropertyPairs(final List<ConditionProperties> conditionProperties,
194                                                  final String queryProperty) {
195         final Map<String, String> result = new HashMap<>();
196         getConditions(conditionProperties, queryProperty).forEach(result::putAll);
197         return result;
198     }
199
200     private List<Map<String, String>> getConditions(final List<ConditionProperties> conditionProperties,
201                                                     final String name) {
202         for (final ConditionProperties conditionProperty : conditionProperties) {
203             if (conditionProperty.getConditionName().equals(name)) {
204                 return conditionProperty.getConditionParameters();
205             }
206         }
207         return Collections.emptyList();
208     }
209
210     private Collection<String> getAllCmHandleReferences(final boolean outputAlternateId) {
211         return cmHandleQueryService.getAllCmHandleReferences(outputAlternateId);
212     }
213
214     private Flux<NcmpServiceCmHandle> getNcmpServiceCmHandles(final Collection<String> cmHandleIds) {
215         return Flux.fromIterable(cmHandleIds)
216                 .buffer(FLUX_BUFFER_SIZE)
217                 .map(this::getNcmpServiceCmHandleBatch)
218                 .flatMap(Flux::fromIterable);
219     }
220
221     private Collection<NcmpServiceCmHandle> getNcmpServiceCmHandleBatch(final Collection<String> cmHandleIds) {
222         final Collection<YangModelCmHandle> yangModelcmHandles
223                 = inventoryPersistence.getYangModelCmHandles(cmHandleIds);
224
225         final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles = new ArrayList<>(yangModelcmHandles.size());
226
227         yangModelcmHandles.forEach(yangModelcmHandle ->
228                 ncmpServiceCmHandles.add(YangDataConverter.toNcmpServiceCmHandle(yangModelcmHandle))
229         );
230         trustLevelManager.applyEffectiveTrustLevels(ncmpServiceCmHandles);
231         return ncmpServiceCmHandles;
232     }
233
234     private Collection<String> executeQueries(final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
235                                               final boolean outputAlternateId,
236                                               final BiFunction<CmHandleQueryServiceParameters, Boolean,
237                                                       Collection<String>>... queryFunctions) {
238         if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
239             return getAllCmHandleReferences(outputAlternateId);
240         }
241         Collection<String> combinedQueryResult = NO_QUERY_TO_EXECUTE;
242         for (final BiFunction<CmHandleQueryServiceParameters, Boolean,
243                 Collection<String>> queryFunction : queryFunctions) {
244             final Collection<String> queryResult = queryFunction.apply(cmHandleQueryServiceParameters,
245                     outputAlternateId);
246             if (noEntriesFoundCanStopQuerying(queryResult)) {
247                 return Collections.emptySet();
248             }
249             combinedQueryResult = combineCmHandleQueryResults(combinedQueryResult, queryResult);
250         }
251         return combinedQueryResult;
252     }
253
254     private boolean noEntriesFoundCanStopQuerying(final Collection<String> queryResult) {
255         return queryResult != NO_QUERY_TO_EXECUTE && queryResult.isEmpty();
256     }
257
258     private Collection<String> combineCmHandleQueryResults(final Collection<String> firstQuery,
259                                                            final Collection<String> secondQuery) {
260         if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) {
261             return NO_QUERY_TO_EXECUTE;
262         } else if (firstQuery == NO_QUERY_TO_EXECUTE) {
263             return secondQuery;
264         } else if (secondQuery == NO_QUERY_TO_EXECUTE) {
265             return firstQuery;
266         } else {
267             firstQuery.retainAll(secondQuery);
268             return firstQuery;
269         }
270     }
271
272 }