2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 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.utils.YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle;
24 import static org.onap.cps.utils.CmHandleQueryRestParametersValidator.validateModuleNameConditionProperties;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
34 import java.util.stream.Collectors;
35 import lombok.RequiredArgsConstructor;
36 import lombok.extern.slf4j.Slf4j;
37 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService;
38 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
39 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
40 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
41 import org.onap.cps.spi.model.Anchor;
42 import org.onap.cps.spi.model.CmHandleQueryServiceParameters;
43 import org.onap.cps.spi.model.ConditionProperties;
44 import org.onap.cps.spi.model.DataNode;
45 import org.springframework.stereotype.Service;
49 @RequiredArgsConstructor
50 public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCmHandlerQueryService {
52 private static final String PROPERTY_QUERY_NAME = "hasAllProperties";
53 private static final String MODULE_QUERY_NAME = "hasAllModules";
54 private static final Map<String, NcmpServiceCmHandle> NO_QUERY_EXECUTED = null;
55 private final InventoryPersistence inventoryPersistence;
58 * Query and return cm handles that match the given query parameters.
60 * @param cmHandleQueryServiceParameters the cm handle query parameters
61 * @return collection of cm handles
64 public Set<NcmpServiceCmHandle> queryCmHandles(
65 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
67 if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
68 return getAllCmHandles();
71 final Map<String, NcmpServiceCmHandle> publicPropertyQueryResult
72 = executePublicPropertyQueries(cmHandleQueryServiceParameters);
74 final Map<String, NcmpServiceCmHandle> combinedQueryResult =
75 combineWithModuleNameQuery(cmHandleQueryServiceParameters, publicPropertyQueryResult);
77 return combinedQueryResult == NO_QUERY_EXECUTED
78 ? Collections.emptySet() : new HashSet<>(combinedQueryResult.values());
82 * Query and return cm handles that match the given query parameters.
84 * @param cmHandleQueryServiceParameters the cm handle query parameters
85 * @return collection of cm handle ids
88 public Set<String> queryCmHandleIds(
89 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
91 if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
92 return getAllCmHandleIds();
95 final Map<String, NcmpServiceCmHandle> publicPropertyQueryResult
96 = executePublicPropertyQueries(cmHandleQueryServiceParameters);
98 final Collection<String> moduleNamesForQuery =
99 getModuleNamesForQuery(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
100 if (moduleNamesForQuery.isEmpty()) {
101 return publicPropertyQueryResult == NO_QUERY_EXECUTED
102 ? Collections.emptySet() : publicPropertyQueryResult.keySet();
104 final Set<String> moduleNameQueryResult = getNamesOfAnchorsWithGivenModules(moduleNamesForQuery);
106 if (publicPropertyQueryResult == NO_QUERY_EXECUTED) {
107 return moduleNameQueryResult;
110 moduleNameQueryResult.retainAll(publicPropertyQueryResult.keySet());
111 return moduleNameQueryResult;
114 private Map<String, NcmpServiceCmHandle> executePublicPropertyQueries(
115 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
116 final Map<String, String> publicPropertyQueryPairs =
117 getPublicPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
118 if (publicPropertyQueryPairs.isEmpty()) {
119 return NO_QUERY_EXECUTED;
121 Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandles = null;
122 for (final Map.Entry<String, String> entry : publicPropertyQueryPairs.entrySet()) {
123 final String cpsPath = "//public-properties[@name='" + entry.getKey() + "' and @value='"
124 + entry.getValue() + "']/ancestor::cm-handles";
126 final Collection<DataNode> dataNodes = inventoryPersistence.queryDataNodes(cpsPath);
127 if (cmHandleIdToNcmpServiceCmHandles == NO_QUERY_EXECUTED) {
128 cmHandleIdToNcmpServiceCmHandles = collectDataNodesToNcmpServiceCmHandles(dataNodes);
130 final Collection<String> cmHandleIdsToRetain = dataNodes.parallelStream()
131 .map(dataNode -> dataNode.getLeaves().get("id").toString()).collect(Collectors.toSet());
132 cmHandleIdToNcmpServiceCmHandles.keySet().retainAll(cmHandleIdsToRetain);
134 if (cmHandleIdToNcmpServiceCmHandles.isEmpty()) {
138 return cmHandleIdToNcmpServiceCmHandles;
141 private Map<String, NcmpServiceCmHandle> combineWithModuleNameQuery(
142 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
143 final Map<String, NcmpServiceCmHandle> previousQueryResult) {
144 final Collection<String> moduleNamesForQuery =
145 getModuleNamesForQuery(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
146 if (moduleNamesForQuery.isEmpty()) {
147 return previousQueryResult;
149 final Collection<String> cmHandleIdsByModuleName = getNamesOfAnchorsWithGivenModules(moduleNamesForQuery);
150 if (cmHandleIdsByModuleName.isEmpty()) {
151 return Collections.emptyMap();
153 final Map<String, NcmpServiceCmHandle> queryResult = new HashMap<>(cmHandleIdsByModuleName.size());
154 if (previousQueryResult == NO_QUERY_EXECUTED) {
155 cmHandleIdsByModuleName.forEach(cmHandleId ->
156 queryResult.put(cmHandleId, createNcmpServiceCmHandle(
157 inventoryPersistence.getDataNode("/dmi-registry/cm-handles[@id='" + cmHandleId + "']")))
161 previousQueryResult.keySet().retainAll(cmHandleIdsByModuleName);
162 queryResult.putAll(previousQueryResult);
166 private Set<String> getNamesOfAnchorsWithGivenModules(final Collection<String> moduleNamesForQuery) {
167 final Collection<Anchor> anchors = inventoryPersistence.queryAnchors(moduleNamesForQuery);
168 return anchors.parallelStream().map(Anchor::getName).collect(Collectors.toSet());
171 private Map<String, NcmpServiceCmHandle> collectDataNodesToNcmpServiceCmHandles(
172 final Collection<DataNode> dataNodes) {
173 final Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandle = new HashMap<>();
174 dataNodes.forEach(dataNode -> {
175 final NcmpServiceCmHandle ncmpServiceCmHandle = createNcmpServiceCmHandle(dataNode);
176 cmHandleIdToNcmpServiceCmHandle.put(ncmpServiceCmHandle.getCmHandleId(), ncmpServiceCmHandle);
178 return cmHandleIdToNcmpServiceCmHandle;
181 private List<Map<String, String>> getConditions(final List<ConditionProperties> conditionProperties,
183 for (final ConditionProperties conditionProperty : conditionProperties) {
184 if (conditionProperty.getConditionName().equals(name)) {
185 return conditionProperty.getConditionParameters();
188 return Collections.emptyList();
191 private Collection<String> getModuleNamesForQuery(final List<ConditionProperties> conditionProperties) {
192 final List<String> result = new ArrayList<>();
193 getConditions(conditionProperties, MODULE_QUERY_NAME).parallelStream().forEach(
194 conditionProperty -> {
195 validateModuleNameConditionProperties(conditionProperty);
196 result.add(conditionProperty.get("moduleName"));
202 private Map<String, String> getPublicPropertyPairs(final List<ConditionProperties> conditionProperties) {
203 final Map<String, String> result = new HashMap<>();
204 getConditions(conditionProperties, PROPERTY_QUERY_NAME).forEach(result::putAll);
208 private Set<NcmpServiceCmHandle> getAllCmHandles() {
209 return inventoryPersistence.getDataNode("/dmi-registry")
210 .getChildDataNodes().stream().map(this::createNcmpServiceCmHandle).collect(Collectors.toSet());
213 private Set<String> getAllCmHandleIds() {
214 return inventoryPersistence.getAnchors().parallelStream().map(Anchor::getName).collect(Collectors.toSet());
217 private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) {
218 return convertYangModelCmHandleToNcmpServiceCmHandle(YangDataConverter
219 .convertCmHandleToYangModel(dataNode, dataNode.getLeaves().get("id").toString()));