2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2024 Nordix Foundation
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.impl.inventory;
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.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT;
26 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.HAS_ALL_MODULES;
27 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.HAS_ALL_PROPERTIES;
28 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.WITH_CPS_PATH;
29 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.WITH_TRUST_LEVEL;
30 import static org.onap.cps.ncmp.impl.utils.YangDataConverter.toNcmpServiceCmHandle;
31 import static org.onap.cps.spi.FetchDescendantsOption.DIRECT_CHILDREN_ONLY;
32 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.function.BiFunction;
41 import java.util.stream.Collectors;
42 import lombok.RequiredArgsConstructor;
43 import org.onap.cps.cpspath.parser.PathParsingException;
44 import org.onap.cps.ncmp.api.inventory.models.CmHandleQueryServiceParameters;
45 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle;
46 import org.onap.cps.ncmp.impl.inventory.models.InventoryQueryConditions;
47 import org.onap.cps.ncmp.impl.inventory.models.PropertyType;
48 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle;
49 import org.onap.cps.ncmp.impl.utils.YangDataConverter;
50 import org.onap.cps.spi.exceptions.DataValidationException;
51 import org.onap.cps.spi.model.ConditionProperties;
52 import org.onap.cps.spi.model.DataNode;
53 import org.springframework.stereotype.Service;
56 @RequiredArgsConstructor
57 public class ParameterizedCmHandleQueryServiceImpl implements ParameterizedCmHandleQueryService {
59 private static final Collection<String> NO_QUERY_TO_EXECUTE = null;
60 private final CmHandleQueryService cmHandleQueryService;
61 private final InventoryPersistence inventoryPersistence;
64 public Collection<String> queryCmHandleReferenceIds(
65 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
66 final boolean outputAlternateId) {
67 return executeQueries(cmHandleQueryServiceParameters, outputAlternateId,
68 this::executeCpsPathQuery,
69 this::queryCmHandlesByPublicProperties,
70 this::executeModuleNameQuery,
71 this::queryCmHandlesByTrustLevel);
75 public Collection<String> queryCmHandleIdsForInventory(
76 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
77 final boolean outputAlternateId) {
78 return executeQueries(cmHandleQueryServiceParameters, outputAlternateId,
79 this::executeCpsPathQuery,
80 this::queryCmHandlesByPublicProperties,
81 this::queryCmHandlesByPrivateProperties,
82 this::queryCmHandlesByDmiPlugin);
86 public Collection<NcmpServiceCmHandle> queryCmHandles(
87 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
89 if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
90 return getAllCmHandles();
93 final Collection<String> cmHandleIds = queryCmHandleReferenceIds(cmHandleQueryServiceParameters, false);
95 return getNcmpServiceCmHandles(cmHandleIds);
99 public Collection<NcmpServiceCmHandle> getAllCmHandles() {
100 final DataNode dataNode = inventoryPersistence.getDataNode(NCMP_DMI_REGISTRY_PARENT).iterator().next();
101 return dataNode.getChildDataNodes().stream().map(this::createNcmpServiceCmHandle).collect(Collectors.toSet());
104 private Collection<String> queryCmHandlesByDmiPlugin(
105 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final boolean outputAlternateId) {
106 final Map<String, String> dmiPropertyQueryPairs =
107 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
108 InventoryQueryConditions.CM_HANDLE_WITH_DMI_PLUGIN.getName());
109 if (dmiPropertyQueryPairs.isEmpty()) {
110 return NO_QUERY_TO_EXECUTE;
113 final String dmiPluginIdentifierValue = dmiPropertyQueryPairs
114 .get(PropertyType.DMI_PLUGIN.getYangContainerName());
116 if (outputAlternateId) {
118 cmHandleQueryService.getCmHandleReferencesMapByDmiPluginIdentifier(dmiPluginIdentifierValue).values();
120 return cmHandleQueryService.getCmHandleReferencesByDmiPluginIdentifier(dmiPluginIdentifierValue,
125 private Collection<String> queryCmHandlesByPrivateProperties(
126 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final boolean outputAlternateId) {
128 final Map<String, String> privatePropertyQueryPairs =
129 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
130 InventoryQueryConditions.HAS_ALL_ADDITIONAL_PROPERTIES.getName());
132 if (privatePropertyQueryPairs.isEmpty()) {
133 return NO_QUERY_TO_EXECUTE;
135 return cmHandleQueryService.queryCmHandleAdditionalProperties(privatePropertyQueryPairs, outputAlternateId);
138 private Collection<String> queryCmHandlesByPublicProperties(
139 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final boolean outputAlternateId) {
141 final Map<String, String> publicPropertyQueryPairs =
142 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
143 HAS_ALL_PROPERTIES.getConditionName());
145 if (publicPropertyQueryPairs.isEmpty()) {
146 return NO_QUERY_TO_EXECUTE;
148 return cmHandleQueryService.queryCmHandlePublicProperties(publicPropertyQueryPairs, outputAlternateId);
151 private Collection<String> queryCmHandlesByTrustLevel(final CmHandleQueryServiceParameters
152 cmHandleQueryServiceParameters,
153 final boolean outputAlternateId) {
155 final Map<String, String> trustLevelPropertyQueryPairs =
156 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
157 WITH_TRUST_LEVEL.getConditionName());
159 if (trustLevelPropertyQueryPairs.isEmpty()) {
160 return NO_QUERY_TO_EXECUTE;
162 return cmHandleQueryService.queryCmHandlesByTrustLevel(trustLevelPropertyQueryPairs, outputAlternateId);
165 private Collection<String> executeModuleNameQuery(
166 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final boolean outputAlternateId) {
167 final Collection<String> moduleNamesForQuery =
168 getModuleNamesForQuery(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
169 if (moduleNamesForQuery.isEmpty()) {
170 return NO_QUERY_TO_EXECUTE;
172 return inventoryPersistence.getCmHandleReferencesWithGivenModules(moduleNamesForQuery, outputAlternateId);
175 private Collection<String> executeCpsPathQuery(
176 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final boolean outputAlternateId) {
177 final Map<String, String> cpsPathCondition
178 = getCpsPathCondition(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
179 if (!validateCpsPathConditionProperties(cpsPathCondition)) {
180 return Collections.emptySet();
182 final Collection<String> cpsPathQueryResult;
183 if (cpsPathCondition.isEmpty()) {
184 return NO_QUERY_TO_EXECUTE;
187 cpsPathQueryResult = collectCmHandleReferencesFromDataNodes(
188 cmHandleQueryService.queryCmHandleAncestorsByCpsPath(cpsPathCondition.get("cpsPath"), OMIT_DESCENDANTS),
190 } catch (final PathParsingException pathParsingException) {
191 throw new DataValidationException(pathParsingException.getMessage(), pathParsingException.getDetails(),
192 pathParsingException);
194 return cpsPathQueryResult;
197 private Collection<String> getModuleNamesForQuery(final List<ConditionProperties> conditionProperties) {
198 final List<String> result = new ArrayList<>();
199 getConditions(conditionProperties, HAS_ALL_MODULES.getConditionName()).forEach(
200 conditionProperty -> {
201 validateModuleNameConditionProperties(conditionProperty);
202 result.add(conditionProperty.get("moduleName"));
207 private Map<String, String> getCpsPathCondition(final List<ConditionProperties> conditionProperties) {
208 final Map<String, String> result = new HashMap<>();
209 getConditions(conditionProperties, WITH_CPS_PATH.getConditionName()).forEach(result::putAll);
213 private Map<String, String> getPropertyPairs(final List<ConditionProperties> conditionProperties,
214 final String queryProperty) {
215 final Map<String, String> result = new HashMap<>();
216 getConditions(conditionProperties, queryProperty).forEach(result::putAll);
220 private List<Map<String, String>> getConditions(final List<ConditionProperties> conditionProperties,
222 for (final ConditionProperties conditionProperty : conditionProperties) {
223 if (conditionProperty.getConditionName().equals(name)) {
224 return conditionProperty.getConditionParameters();
227 return Collections.emptyList();
230 private Collection<String> getAllCmHandleReferences(final boolean outputAlternateId) {
231 final DataNode dataNode = inventoryPersistence.getDataNode(NCMP_DMI_REGISTRY_PARENT, DIRECT_CHILDREN_ONLY)
233 return collectCmHandleReferencesFromDataNodes(dataNode.getChildDataNodes(), outputAlternateId);
236 private Collection<NcmpServiceCmHandle> getNcmpServiceCmHandles(final Collection<String> cmHandleIds) {
237 final Collection<YangModelCmHandle> yangModelcmHandles
238 = inventoryPersistence.getYangModelCmHandles(cmHandleIds);
240 final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles = new ArrayList<>(yangModelcmHandles.size());
242 yangModelcmHandles.forEach(yangModelcmHandle ->
243 ncmpServiceCmHandles.add(YangDataConverter.toNcmpServiceCmHandle(yangModelcmHandle))
245 return ncmpServiceCmHandles;
248 private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) {
249 return toNcmpServiceCmHandle(YangDataConverter.toYangModelCmHandle(dataNode));
252 private Collection<String> executeQueries(final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
253 final boolean outputAlternateId,
254 final BiFunction<CmHandleQueryServiceParameters, Boolean,
255 Collection<String>>... queryFunctions) {
256 if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
257 return getAllCmHandleReferences(outputAlternateId);
259 Collection<String> combinedQueryResult = NO_QUERY_TO_EXECUTE;
260 for (final BiFunction<CmHandleQueryServiceParameters, Boolean,
261 Collection<String>> queryFunction : queryFunctions) {
262 final Collection<String> queryResult = queryFunction.apply(cmHandleQueryServiceParameters,
264 if (noEntriesFoundCanStopQuerying(queryResult)) {
265 return Collections.emptySet();
267 combinedQueryResult = combineCmHandleQueryResults(combinedQueryResult, queryResult);
269 return combinedQueryResult;
272 private boolean noEntriesFoundCanStopQuerying(final Collection<String> queryResult) {
273 return queryResult != NO_QUERY_TO_EXECUTE && queryResult.isEmpty();
276 private Collection<String> combineCmHandleQueryResults(final Collection<String> firstQuery,
277 final Collection<String> secondQuery) {
278 if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) {
279 return NO_QUERY_TO_EXECUTE;
280 } else if (firstQuery == NO_QUERY_TO_EXECUTE) {
282 } else if (secondQuery == NO_QUERY_TO_EXECUTE) {
285 firstQuery.retainAll(secondQuery);
290 private Collection<String> collectCmHandleReferencesFromDataNodes(final Collection<DataNode> dataNodes,
291 final boolean outputAlternateId) {
292 if (outputAlternateId) {
293 return dataNodes.stream().map(dataNode ->
294 (String) dataNode.getLeaves().get("alternate-id")).collect(Collectors.toSet());
296 return dataNodes.stream().map(dataNode ->
297 (String) dataNode.getLeaves().get("id")).collect(Collectors.toSet());