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.Function;
41 import java.util.stream.Collectors;
42 import lombok.RequiredArgsConstructor;
43 import lombok.extern.slf4j.Slf4j;
44 import org.onap.cps.cpspath.parser.PathParsingException;
45 import org.onap.cps.ncmp.api.inventory.models.CmHandleQueryServiceParameters;
46 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle;
47 import org.onap.cps.ncmp.impl.inventory.models.InventoryQueryConditions;
48 import org.onap.cps.ncmp.impl.inventory.models.PropertyType;
49 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle;
50 import org.onap.cps.ncmp.impl.utils.YangDataConverter;
51 import org.onap.cps.spi.exceptions.DataValidationException;
52 import org.onap.cps.spi.model.ConditionProperties;
53 import org.onap.cps.spi.model.DataNode;
54 import org.springframework.stereotype.Service;
58 @RequiredArgsConstructor
59 public class ParameterizedCmHandleQueryServiceImpl implements ParameterizedCmHandleQueryService {
61 private static final Collection<String> NO_QUERY_TO_EXECUTE = null;
62 private final CmHandleQueryService cmHandleQueryService;
63 private final InventoryPersistence inventoryPersistence;
66 public Collection<String> queryCmHandleIds(
67 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
68 return executeQueries(cmHandleQueryServiceParameters,
69 this::executeCpsPathQuery,
70 this::queryCmHandlesByPublicProperties,
71 this::executeModuleNameQuery,
72 this::queryCmHandlesByTrustLevel);
76 public Collection<String> queryCmHandleIdsForInventory(
77 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
78 return executeQueries(cmHandleQueryServiceParameters,
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 = queryCmHandleIds(cmHandleQueryServiceParameters);
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) {
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 return cmHandleQueryService.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifierValue);
119 private Collection<String> queryCmHandlesByPrivateProperties(
120 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
122 final Map<String, String> privatePropertyQueryPairs =
123 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
124 InventoryQueryConditions.HAS_ALL_ADDITIONAL_PROPERTIES.getName());
126 if (privatePropertyQueryPairs.isEmpty()) {
127 return NO_QUERY_TO_EXECUTE;
129 return cmHandleQueryService.queryCmHandleAdditionalProperties(privatePropertyQueryPairs);
132 private Collection<String> queryCmHandlesByPublicProperties(
133 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
135 final Map<String, String> publicPropertyQueryPairs =
136 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
137 HAS_ALL_PROPERTIES.getConditionName());
139 if (publicPropertyQueryPairs.isEmpty()) {
140 return NO_QUERY_TO_EXECUTE;
142 return cmHandleQueryService.queryCmHandlePublicProperties(publicPropertyQueryPairs);
145 private Collection<String> queryCmHandlesByTrustLevel(final CmHandleQueryServiceParameters
146 cmHandleQueryServiceParameters) {
148 final Map<String, String> trustLevelPropertyQueryPairs =
149 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
150 WITH_TRUST_LEVEL.getConditionName());
152 if (trustLevelPropertyQueryPairs.isEmpty()) {
153 return NO_QUERY_TO_EXECUTE;
155 return cmHandleQueryService.queryCmHandlesByTrustLevel(trustLevelPropertyQueryPairs);
158 private Collection<String> executeModuleNameQuery(
159 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
160 final Collection<String> moduleNamesForQuery =
161 getModuleNamesForQuery(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
162 if (moduleNamesForQuery.isEmpty()) {
163 return NO_QUERY_TO_EXECUTE;
165 return inventoryPersistence.getCmHandleIdsWithGivenModules(moduleNamesForQuery);
168 private Collection<String> executeCpsPathQuery(
169 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
170 final Map<String, String> cpsPathCondition
171 = getCpsPathCondition(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
172 if (!validateCpsPathConditionProperties(cpsPathCondition)) {
173 return Collections.emptySet();
175 final Collection<String> cpsPathQueryResult;
176 if (cpsPathCondition.isEmpty()) {
177 return NO_QUERY_TO_EXECUTE;
180 cpsPathQueryResult = collectCmHandleIdsFromDataNodes(
181 cmHandleQueryService.queryCmHandleAncestorsByCpsPath(
182 cpsPathCondition.get("cpsPath"), OMIT_DESCENDANTS));
183 } catch (final PathParsingException pathParsingException) {
184 throw new DataValidationException(pathParsingException.getMessage(), pathParsingException.getDetails(),
185 pathParsingException);
187 return cpsPathQueryResult;
190 private Collection<String> getModuleNamesForQuery(final List<ConditionProperties> conditionProperties) {
191 final List<String> result = new ArrayList<>();
192 getConditions(conditionProperties, HAS_ALL_MODULES.getConditionName()).forEach(
193 conditionProperty -> {
194 validateModuleNameConditionProperties(conditionProperty);
195 result.add(conditionProperty.get("moduleName"));
200 private Map<String, String> getCpsPathCondition(final List<ConditionProperties> conditionProperties) {
201 final Map<String, String> result = new HashMap<>();
202 getConditions(conditionProperties, WITH_CPS_PATH.getConditionName()).forEach(result::putAll);
206 private Map<String, String> getPropertyPairs(final List<ConditionProperties> conditionProperties,
207 final String queryProperty) {
208 final Map<String, String> result = new HashMap<>();
209 getConditions(conditionProperties, queryProperty).forEach(result::putAll);
213 private List<Map<String, String>> getConditions(final List<ConditionProperties> conditionProperties,
215 for (final ConditionProperties conditionProperty : conditionProperties) {
216 if (conditionProperty.getConditionName().equals(name)) {
217 return conditionProperty.getConditionParameters();
220 return Collections.emptyList();
223 private Collection<String> getAllCmHandleIds() {
224 final DataNode dataNode = inventoryPersistence.getDataNode(NCMP_DMI_REGISTRY_PARENT, DIRECT_CHILDREN_ONLY)
226 return collectCmHandleIdsFromDataNodes(dataNode.getChildDataNodes());
229 private Collection<NcmpServiceCmHandle> getNcmpServiceCmHandles(final Collection<String> cmHandleIds) {
230 final Collection<YangModelCmHandle> yangModelcmHandles
231 = inventoryPersistence.getYangModelCmHandles(cmHandleIds);
233 final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles = new ArrayList<>(yangModelcmHandles.size());
235 yangModelcmHandles.forEach(yangModelcmHandle ->
236 ncmpServiceCmHandles.add(YangDataConverter.toNcmpServiceCmHandle(yangModelcmHandle))
238 return ncmpServiceCmHandles;
241 private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) {
242 return toNcmpServiceCmHandle(YangDataConverter.toYangModelCmHandle(dataNode));
245 private Collection<String> executeQueries(final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
246 final Function<CmHandleQueryServiceParameters, Collection<String>>...
248 if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
249 return getAllCmHandleIds();
251 Collection<String> combinedQueryResult = NO_QUERY_TO_EXECUTE;
252 for (final Function<CmHandleQueryServiceParameters, Collection<String>> queryFunction : queryFunctions) {
253 final Collection<String> queryResult = queryFunction.apply(cmHandleQueryServiceParameters);
254 if (noEntriesFoundCanStopQuerying(queryResult)) {
255 return Collections.emptySet();
257 combinedQueryResult = combineCmHandleQueryResults(combinedQueryResult, queryResult);
259 return combinedQueryResult;
262 private boolean noEntriesFoundCanStopQuerying(final Collection<String> queryResult) {
263 return queryResult != NO_QUERY_TO_EXECUTE && queryResult.isEmpty();
266 private Collection<String> combineCmHandleQueryResults(final Collection<String> firstQuery,
267 final Collection<String> secondQuery) {
268 if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) {
269 return NO_QUERY_TO_EXECUTE;
270 } else if (firstQuery == NO_QUERY_TO_EXECUTE) {
272 } else if (secondQuery == NO_QUERY_TO_EXECUTE) {
275 firstQuery.retainAll(secondQuery);
280 private Collection<String> collectCmHandleIdsFromDataNodes(final Collection<DataNode> dataNodes) {
281 return dataNodes.stream().map(dataNode -> (String) dataNode.getLeaves().get("id")).collect(Collectors.toSet());