2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 Nordix Foundation
4 * Modifications Copyright (C) 2023 TechMahindra Ltd.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.impl;
24 import static org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions.HAS_ALL_MODULES;
25 import static org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions.HAS_ALL_PROPERTIES;
26 import static org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions.WITH_CPS_PATH;
27 import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateCpsPathConditionProperties;
28 import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateModuleNameConditionProperties;
29 import static org.onap.cps.ncmp.api.impl.utils.YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle;
30 import static org.onap.cps.spi.FetchDescendantsOption.DIRECT_CHILD_ONLY;
31 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.List;
39 import java.util.function.Function;
40 import java.util.stream.Collectors;
41 import lombok.RequiredArgsConstructor;
42 import lombok.extern.slf4j.Slf4j;
43 import org.onap.cps.cpspath.parser.PathParsingException;
44 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandleQueryService;
45 import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions;
46 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
47 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
48 import org.onap.cps.ncmp.api.inventory.CmHandleQueries;
49 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
50 import org.onap.cps.ncmp.api.inventory.enums.PropertyType;
51 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters;
52 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
53 import org.onap.cps.spi.exceptions.DataValidationException;
54 import org.onap.cps.spi.model.ConditionProperties;
55 import org.onap.cps.spi.model.DataNode;
56 import org.springframework.stereotype.Service;
60 @RequiredArgsConstructor
61 public class NetworkCmProxyCmHandleQueryServiceImpl implements NetworkCmProxyCmHandleQueryService {
63 private static final Collection<String> NO_QUERY_TO_EXECUTE = null;
64 private final CmHandleQueries cmHandleQueries;
65 private final InventoryPersistence inventoryPersistence;
68 public Collection<String> queryCmHandleIds(
69 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
70 return executeQueries(cmHandleQueryServiceParameters,
71 this::executeCpsPathQuery,
72 this::queryCmHandlesByPublicProperties,
73 this::executeModuleNameQuery);
77 public Collection<String> queryCmHandleIdsForInventory(
78 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
79 return executeQueries(cmHandleQueryServiceParameters,
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);
98 private Collection<String> queryCmHandlesByDmiPlugin(
99 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
100 final Map<String, String> dmiPropertyQueryPairs =
101 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
102 InventoryQueryConditions.CM_HANDLE_WITH_DMI_PLUGIN.getName());
103 if (dmiPropertyQueryPairs.isEmpty()) {
104 return NO_QUERY_TO_EXECUTE;
107 final String dmiPluginIdentifierValue = dmiPropertyQueryPairs
108 .get(PropertyType.DMI_PLUGIN.getYangContainerName());
110 return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifierValue);
113 private Collection<String> queryCmHandlesByPrivateProperties(
114 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
116 final Map<String, String> privatePropertyQueryPairs =
117 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
118 InventoryQueryConditions.HAS_ALL_ADDITIONAL_PROPERTIES.getName());
120 return privatePropertyQueryPairs.isEmpty()
121 ? NO_QUERY_TO_EXECUTE
122 : cmHandleQueries.queryCmHandleAdditionalProperties(privatePropertyQueryPairs);
125 private Collection<String> queryCmHandlesByPublicProperties(
126 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
128 final Map<String, String> publicPropertyQueryPairs =
129 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
130 HAS_ALL_PROPERTIES.getConditionName());
132 return publicPropertyQueryPairs.isEmpty()
133 ? NO_QUERY_TO_EXECUTE
134 : cmHandleQueries.queryCmHandlePublicProperties(publicPropertyQueryPairs);
137 private Collection<String> executeModuleNameQuery(
138 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
139 final Collection<String> moduleNamesForQuery =
140 getModuleNamesForQuery(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
141 if (moduleNamesForQuery.isEmpty()) {
142 return NO_QUERY_TO_EXECUTE;
144 return inventoryPersistence.getCmHandleIdsWithGivenModules(moduleNamesForQuery);
147 private Collection<String> executeCpsPathQuery(
148 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
149 final Map<String, String> cpsPathCondition
150 = getCpsPathCondition(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
151 if (!validateCpsPathConditionProperties(cpsPathCondition)) {
152 return Collections.emptySet();
154 final Collection<String> cpsPathQueryResult;
155 if (cpsPathCondition.isEmpty()) {
156 return NO_QUERY_TO_EXECUTE;
159 cpsPathQueryResult = collectCmHandleIdsFromDataNodes(
160 cmHandleQueries.queryCmHandleDataNodesByCpsPath(cpsPathCondition.get("cpsPath"), OMIT_DESCENDANTS));
161 } catch (final PathParsingException pathParsingException) {
162 throw new DataValidationException(pathParsingException.getMessage(), pathParsingException.getDetails(),
163 pathParsingException);
165 return cpsPathQueryResult;
168 private Collection<String> getModuleNamesForQuery(final List<ConditionProperties> conditionProperties) {
169 final List<String> result = new ArrayList<>();
170 getConditions(conditionProperties, HAS_ALL_MODULES.getConditionName()).forEach(
171 conditionProperty -> {
172 validateModuleNameConditionProperties(conditionProperty);
173 result.add(conditionProperty.get("moduleName"));
178 private Map<String, String> getCpsPathCondition(final List<ConditionProperties> conditionProperties) {
179 final Map<String, String> result = new HashMap<>();
180 getConditions(conditionProperties, WITH_CPS_PATH.getConditionName()).forEach(result::putAll);
184 private Map<String, String> getPropertyPairs(final List<ConditionProperties> conditionProperties,
185 final String queryProperty) {
186 final Map<String, String> result = new HashMap<>();
187 getConditions(conditionProperties, queryProperty).forEach(result::putAll);
191 private List<Map<String, String>> getConditions(final List<ConditionProperties> conditionProperties,
193 for (final ConditionProperties conditionProperty : conditionProperties) {
194 if (conditionProperty.getConditionName().equals(name)) {
195 return conditionProperty.getConditionParameters();
198 return Collections.emptyList();
201 private Collection<NcmpServiceCmHandle> getAllCmHandles() {
202 final DataNode dataNode = inventoryPersistence.getDataNode("/dmi-registry").iterator().next();
203 return dataNode.getChildDataNodes().stream().map(this::createNcmpServiceCmHandle).collect(Collectors.toSet());
206 private Collection<String> getAllCmHandleIds() {
207 final DataNode dataNode = inventoryPersistence.getDataNode("/dmi-registry", DIRECT_CHILD_ONLY)
209 return collectCmHandleIdsFromDataNodes(dataNode.getChildDataNodes());
212 private Collection<NcmpServiceCmHandle> getNcmpServiceCmHandles(final Collection<String> cmHandleIds) {
213 final Collection<YangModelCmHandle> yangModelcmHandles
214 = inventoryPersistence.getYangModelCmHandles(cmHandleIds);
216 final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles = new ArrayList<>(yangModelcmHandles.size());
218 yangModelcmHandles.forEach(yangModelcmHandle ->
219 ncmpServiceCmHandles.add(YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(yangModelcmHandle))
221 return ncmpServiceCmHandles;
224 private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) {
225 return convertYangModelCmHandleToNcmpServiceCmHandle(YangDataConverter
226 .convertCmHandleToYangModel(dataNode, dataNode.getLeaves().get("id").toString()));
229 private Collection<String> executeQueries(final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
230 final Function<CmHandleQueryServiceParameters, Collection<String>>...
232 if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
233 return getAllCmHandleIds();
235 Collection<String> combinedQueryResult = NO_QUERY_TO_EXECUTE;
236 for (final Function<CmHandleQueryServiceParameters, Collection<String>> queryFunction : queryFunctions) {
237 final Collection<String> queryResult = queryFunction.apply(cmHandleQueryServiceParameters);
238 if (noEntriesFoundCanStopQuerying(queryResult)) {
239 return Collections.emptySet();
241 combinedQueryResult = combineCmHandleQueryResults(combinedQueryResult, queryResult);
243 return combinedQueryResult;
246 private boolean noEntriesFoundCanStopQuerying(final Collection<String> queryResult) {
247 return queryResult != NO_QUERY_TO_EXECUTE && queryResult.isEmpty();
250 private Collection<String> combineCmHandleQueryResults(final Collection<String> firstQuery,
251 final Collection<String> secondQuery) {
252 if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) {
253 return NO_QUERY_TO_EXECUTE;
254 } else if (firstQuery == NO_QUERY_TO_EXECUTE) {
256 } else if (secondQuery == NO_QUERY_TO_EXECUTE) {
259 firstQuery.retainAll(secondQuery);
264 private Collection<String> collectCmHandleIdsFromDataNodes(final Collection<DataNode> dataNodes) {
265 return dataNodes.stream().map(dataNode -> (String) dataNode.getLeaves().get("id")).collect(Collectors.toSet());