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.api.impl;
23 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT;
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.CmHandleQueryConditions.WITH_TRUST_LEVEL;
28 import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateCpsPathConditionProperties;
29 import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateModuleNameConditionProperties;
30 import static org.onap.cps.ncmp.api.impl.utils.YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle;
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.NetworkCmProxyCmHandleQueryService;
46 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries;
47 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
48 import org.onap.cps.ncmp.api.impl.inventory.enums.PropertyType;
49 import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions;
50 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
51 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
52 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters;
53 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
54 import org.onap.cps.spi.exceptions.DataValidationException;
55 import org.onap.cps.spi.model.ConditionProperties;
56 import org.onap.cps.spi.model.DataNode;
57 import org.springframework.stereotype.Service;
61 @RequiredArgsConstructor
62 public class NetworkCmProxyCmHandleQueryServiceImpl implements NetworkCmProxyCmHandleQueryService {
64 private static final Collection<String> NO_QUERY_TO_EXECUTE = null;
65 private final CmHandleQueries cmHandleQueries;
66 private final InventoryPersistence inventoryPersistence;
69 public Collection<String> queryCmHandleIds(
70 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
71 return executeQueries(cmHandleQueryServiceParameters,
72 this::executeCpsPathQuery,
73 this::queryCmHandlesByPublicProperties,
74 this::executeModuleNameQuery,
75 this::queryCmHandlesByTrustLevel);
79 public Collection<String> queryCmHandleIdsForInventory(
80 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
81 return executeQueries(cmHandleQueryServiceParameters,
82 this::queryCmHandlesByPublicProperties,
83 this::queryCmHandlesByPrivateProperties,
84 this::queryCmHandlesByDmiPlugin);
88 public Collection<NcmpServiceCmHandle> queryCmHandles(
89 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
91 if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
92 return getAllCmHandles();
95 final Collection<String> cmHandleIds = queryCmHandleIds(cmHandleQueryServiceParameters);
97 return getNcmpServiceCmHandles(cmHandleIds);
101 public Collection<NcmpServiceCmHandle> getAllCmHandles() {
102 final DataNode dataNode = inventoryPersistence.getDataNode(NCMP_DMI_REGISTRY_PARENT).iterator().next();
103 return dataNode.getChildDataNodes().stream().map(this::createNcmpServiceCmHandle).collect(Collectors.toSet());
106 private Collection<String> queryCmHandlesByDmiPlugin(
107 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
108 final Map<String, String> dmiPropertyQueryPairs =
109 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
110 InventoryQueryConditions.CM_HANDLE_WITH_DMI_PLUGIN.getName());
111 if (dmiPropertyQueryPairs.isEmpty()) {
112 return NO_QUERY_TO_EXECUTE;
115 final String dmiPluginIdentifierValue = dmiPropertyQueryPairs
116 .get(PropertyType.DMI_PLUGIN.getYangContainerName());
118 return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifierValue);
121 private Collection<String> queryCmHandlesByPrivateProperties(
122 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
124 final Map<String, String> privatePropertyQueryPairs =
125 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
126 InventoryQueryConditions.HAS_ALL_ADDITIONAL_PROPERTIES.getName());
128 if (privatePropertyQueryPairs.isEmpty()) {
129 return NO_QUERY_TO_EXECUTE;
131 return cmHandleQueries.queryCmHandleAdditionalProperties(privatePropertyQueryPairs);
134 private Collection<String> queryCmHandlesByPublicProperties(
135 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
137 final Map<String, String> publicPropertyQueryPairs =
138 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
139 HAS_ALL_PROPERTIES.getConditionName());
141 if (publicPropertyQueryPairs.isEmpty()) {
142 return NO_QUERY_TO_EXECUTE;
144 return cmHandleQueries.queryCmHandlePublicProperties(publicPropertyQueryPairs);
147 private Collection<String> queryCmHandlesByTrustLevel(final CmHandleQueryServiceParameters
148 cmHandleQueryServiceParameters) {
150 final Map<String, String> trustLevelPropertyQueryPairs =
151 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
152 WITH_TRUST_LEVEL.getConditionName());
154 if (trustLevelPropertyQueryPairs.isEmpty()) {
155 return NO_QUERY_TO_EXECUTE;
157 return cmHandleQueries.queryCmHandlesByTrustLevel(trustLevelPropertyQueryPairs);
160 private Collection<String> executeModuleNameQuery(
161 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
162 final Collection<String> moduleNamesForQuery =
163 getModuleNamesForQuery(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
164 if (moduleNamesForQuery.isEmpty()) {
165 return NO_QUERY_TO_EXECUTE;
167 return inventoryPersistence.getCmHandleIdsWithGivenModules(moduleNamesForQuery);
170 private Collection<String> executeCpsPathQuery(
171 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
172 final Map<String, String> cpsPathCondition
173 = getCpsPathCondition(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
174 if (!validateCpsPathConditionProperties(cpsPathCondition)) {
175 return Collections.emptySet();
177 final Collection<String> cpsPathQueryResult;
178 if (cpsPathCondition.isEmpty()) {
179 return NO_QUERY_TO_EXECUTE;
182 cpsPathQueryResult = collectCmHandleIdsFromDataNodes(
183 cmHandleQueries.queryCmHandleAncestorsByCpsPath(
184 cpsPathCondition.get("cpsPath"), OMIT_DESCENDANTS));
185 } catch (final PathParsingException pathParsingException) {
186 throw new DataValidationException(pathParsingException.getMessage(), pathParsingException.getDetails(),
187 pathParsingException);
189 return cpsPathQueryResult;
192 private Collection<String> getModuleNamesForQuery(final List<ConditionProperties> conditionProperties) {
193 final List<String> result = new ArrayList<>();
194 getConditions(conditionProperties, HAS_ALL_MODULES.getConditionName()).forEach(
195 conditionProperty -> {
196 validateModuleNameConditionProperties(conditionProperty);
197 result.add(conditionProperty.get("moduleName"));
202 private Map<String, String> getCpsPathCondition(final List<ConditionProperties> conditionProperties) {
203 final Map<String, String> result = new HashMap<>();
204 getConditions(conditionProperties, WITH_CPS_PATH.getConditionName()).forEach(result::putAll);
208 private Map<String, String> getPropertyPairs(final List<ConditionProperties> conditionProperties,
209 final String queryProperty) {
210 final Map<String, String> result = new HashMap<>();
211 getConditions(conditionProperties, queryProperty).forEach(result::putAll);
215 private List<Map<String, String>> getConditions(final List<ConditionProperties> conditionProperties,
217 for (final ConditionProperties conditionProperty : conditionProperties) {
218 if (conditionProperty.getConditionName().equals(name)) {
219 return conditionProperty.getConditionParameters();
222 return Collections.emptyList();
225 private Collection<String> getAllCmHandleIds() {
226 final DataNode dataNode = inventoryPersistence.getDataNode(NCMP_DMI_REGISTRY_PARENT, DIRECT_CHILDREN_ONLY)
228 return collectCmHandleIdsFromDataNodes(dataNode.getChildDataNodes());
231 private Collection<NcmpServiceCmHandle> getNcmpServiceCmHandles(final Collection<String> cmHandleIds) {
232 final Collection<YangModelCmHandle> yangModelcmHandles
233 = inventoryPersistence.getYangModelCmHandles(cmHandleIds);
235 final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles = new ArrayList<>(yangModelcmHandles.size());
237 yangModelcmHandles.forEach(yangModelcmHandle ->
238 ncmpServiceCmHandles.add(YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(yangModelcmHandle))
240 return ncmpServiceCmHandles;
243 private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) {
244 return convertYangModelCmHandleToNcmpServiceCmHandle(YangDataConverter
245 .convertCmHandleToYangModel(dataNode, dataNode.getLeaves().get("id").toString()));
248 private Collection<String> executeQueries(final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
249 final Function<CmHandleQueryServiceParameters, Collection<String>>...
251 if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
252 return getAllCmHandleIds();
254 Collection<String> combinedQueryResult = NO_QUERY_TO_EXECUTE;
255 for (final Function<CmHandleQueryServiceParameters, Collection<String>> queryFunction : queryFunctions) {
256 final Collection<String> queryResult = queryFunction.apply(cmHandleQueryServiceParameters);
257 if (noEntriesFoundCanStopQuerying(queryResult)) {
258 return Collections.emptySet();
260 combinedQueryResult = combineCmHandleQueryResults(combinedQueryResult, queryResult);
262 return combinedQueryResult;
265 private boolean noEntriesFoundCanStopQuerying(final Collection<String> queryResult) {
266 return queryResult != NO_QUERY_TO_EXECUTE && queryResult.isEmpty();
269 private Collection<String> combineCmHandleQueryResults(final Collection<String> firstQuery,
270 final Collection<String> secondQuery) {
271 if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) {
272 return NO_QUERY_TO_EXECUTE;
273 } else if (firstQuery == NO_QUERY_TO_EXECUTE) {
275 } else if (secondQuery == NO_QUERY_TO_EXECUTE) {
278 firstQuery.retainAll(secondQuery);
283 private Collection<String> collectCmHandleIdsFromDataNodes(final Collection<DataNode> dataNodes) {
284 return dataNodes.stream().map(dataNode -> (String) dataNode.getLeaves().get("id")).collect(Collectors.toSet());