d64dfb4a63c2577df5f9813f30ecdb087deeddc9
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / NetworkCmProxyCmHandlerQueryServiceImpl.java
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl;
22
23 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DATASPACE_NAME;
24 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_ANCHOR;
25 import static org.onap.cps.ncmp.api.impl.utils.YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle;
26 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
27 import static org.onap.cps.utils.CmHandleQueryRestParametersValidator.validateModuleNameConditionProperties;
28
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37 import java.util.stream.Collectors;
38 import lombok.RequiredArgsConstructor;
39 import lombok.extern.slf4j.Slf4j;
40 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService;
41 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
42 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
43 import org.onap.cps.spi.CpsAdminPersistenceService;
44 import org.onap.cps.spi.CpsDataPersistenceService;
45 import org.onap.cps.spi.model.Anchor;
46 import org.onap.cps.spi.model.CmHandleQueryServiceParameters;
47 import org.onap.cps.spi.model.ConditionProperties;
48 import org.onap.cps.spi.model.DataNode;
49 import org.springframework.stereotype.Service;
50
51 @Service
52 @Slf4j
53 @RequiredArgsConstructor
54 public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCmHandlerQueryService {
55
56     private static final String PROPERTY_QUERY_NAME = "hasAllProperties";
57     private static final String MODULE_QUERY_NAME = "hasAllModules";
58     private static final Object NO_QUERY_EXECUTED = null;
59     private final CpsDataPersistenceService cpsDataPersistenceService;
60     private final CpsAdminPersistenceService cpsAdminPersistenceService;
61
62     /**
63      * Query and return cm handles that match the given query parameters.
64      *
65      * @param cmHandleQueryServiceParameters the cm handle query parameters
66      * @return collection of cm handles
67      */
68     @Override
69     public Set<NcmpServiceCmHandle> queryCmHandles(
70         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
71
72         if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
73             return getAllCmHandles();
74         }
75
76         final Map<String, NcmpServiceCmHandle> publicPropertyQueryResult
77             = executePublicPropertyQueries(cmHandleQueryServiceParameters);
78
79         final Map<String, NcmpServiceCmHandle> combinedQueryResult =
80             combineWithModuleNameQuery(cmHandleQueryServiceParameters, publicPropertyQueryResult);
81
82         return combinedQueryResult == NO_QUERY_EXECUTED
83             ? Collections.emptySet() : new HashSet<NcmpServiceCmHandle>(combinedQueryResult.values());
84     }
85
86     /**
87      * Query and return cm handles that match the given query parameters.
88      *
89      * @param cmHandleQueryServiceParameters the cm handle query parameters
90      * @return collection of cm handle ids
91      */
92     @Override
93     public Set<String> queryCmHandleIds(
94         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
95
96         if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
97             return getAllCmHandleIds();
98         }
99
100         final Map<String, NcmpServiceCmHandle> publicPropertyQueryResult
101             = executePublicPropertyQueries(cmHandleQueryServiceParameters);
102
103         final Collection<String> moduleNamesForQuery =
104             getModuleNamesForQuery(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
105         if (moduleNamesForQuery.isEmpty()) {
106             return publicPropertyQueryResult == NO_QUERY_EXECUTED
107                 ? Collections.emptySet() : publicPropertyQueryResult.keySet();
108         }
109         final Set<String> moduleNameQueryResult = getNamesOfAnchorsWithGivenModules(moduleNamesForQuery);
110
111         if (publicPropertyQueryResult == NO_QUERY_EXECUTED) {
112             return moduleNameQueryResult;
113         }
114
115         moduleNameQueryResult.retainAll(publicPropertyQueryResult.keySet());
116         return moduleNameQueryResult;
117     }
118
119     private Map<String, NcmpServiceCmHandle> executePublicPropertyQueries(
120         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
121         final Map<String, String> publicPropertyQueryPairs =
122             getPublicPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
123         if (publicPropertyQueryPairs.isEmpty()) {
124             return null;
125         }
126         Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandles = null;
127         for (final Map.Entry<String, String> entry : publicPropertyQueryPairs.entrySet()) {
128             final String cmHandlePath = "//public-properties[@name='" + entry.getKey() + "' and @value='"
129                 + entry.getValue() + "']/ancestor::cm-handles";
130
131             final Collection<DataNode> dataNodes = queryDataNodes(cmHandlePath);
132             if (cmHandleIdToNcmpServiceCmHandles == NO_QUERY_EXECUTED) {
133                 cmHandleIdToNcmpServiceCmHandles = collectDataNodesToNcmpServiceCmHandles(dataNodes);
134             } else {
135                 final Collection<String> cmHandleIdsToRetain = dataNodes.parallelStream()
136                     .map(dataNode -> dataNode.getLeaves().get("id").toString()).collect(Collectors.toSet());
137                 cmHandleIdToNcmpServiceCmHandles.keySet().retainAll(cmHandleIdsToRetain);
138             }
139             if (cmHandleIdToNcmpServiceCmHandles.isEmpty()) {
140                 break;
141             }
142         }
143         return cmHandleIdToNcmpServiceCmHandles;
144     }
145
146     private Map<String, NcmpServiceCmHandle> combineWithModuleNameQuery(
147         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
148         final Map<String, NcmpServiceCmHandle> previousQueryResult) {
149         final Collection<String> moduleNamesForQuery =
150             getModuleNamesForQuery(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
151         if (moduleNamesForQuery.isEmpty()) {
152             return previousQueryResult;
153         }
154         final Collection<String> cmHandleIdsByModuleName = getNamesOfAnchorsWithGivenModules(moduleNamesForQuery);
155         if (cmHandleIdsByModuleName.isEmpty()) {
156             return Collections.emptyMap();
157         }
158         final Map<String, NcmpServiceCmHandle> queryResult = new HashMap<>(cmHandleIdsByModuleName.size());
159         if (previousQueryResult == NO_QUERY_EXECUTED) {
160             //TODO Discuss performance/scaling of getting ALL cmHandles here
161             getAllCmHandles().forEach(ncmpServiceCmHandle -> {
162                 if (cmHandleIdsByModuleName.contains(ncmpServiceCmHandle.getCmHandleId())) {
163                     queryResult.put(ncmpServiceCmHandle.getCmHandleId(), ncmpServiceCmHandle);
164                 }
165             });
166             return queryResult;
167         }
168         previousQueryResult.keySet().retainAll(cmHandleIdsByModuleName);
169         queryResult.putAll(previousQueryResult);
170         return queryResult;
171     }
172
173     private Set<String> getNamesOfAnchorsWithGivenModules(final Collection<String> moduleNamesForQuery) {
174         final Collection<Anchor> anchors =
175             cpsAdminPersistenceService.queryAnchors("NFP-Operational", moduleNamesForQuery);
176         return anchors.parallelStream().map(Anchor::getName).collect(Collectors.toSet());
177     }
178
179     private Map<String, NcmpServiceCmHandle> collectDataNodesToNcmpServiceCmHandles(
180         final Collection<DataNode> dataNodes) {
181         final Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandle = new HashMap<>();
182         dataNodes.forEach(dataNode -> {
183             final NcmpServiceCmHandle ncmpServiceCmHandle = createNcmpServiceCmHandle(dataNode);
184             cmHandleIdToNcmpServiceCmHandle.put(ncmpServiceCmHandle.getCmHandleId(), ncmpServiceCmHandle);
185         });
186         return cmHandleIdToNcmpServiceCmHandle;
187     }
188
189     private List<Map<String, String>> getConditions(final List<ConditionProperties> conditionProperties,
190                                                     final String name) {
191         for (final ConditionProperties conditionProperty : conditionProperties) {
192             if (conditionProperty.getConditionName().equals(name)) {
193                 return conditionProperty.getConditionParameters();
194             }
195         }
196         return Collections.emptyList();
197     }
198
199     private Collection<String> getModuleNamesForQuery(final List<ConditionProperties> conditionProperties) {
200         final List<String> result = new ArrayList<>();
201         getConditions(conditionProperties, MODULE_QUERY_NAME).parallelStream().forEach(
202             conditionProperty -> {
203                 validateModuleNameConditionProperties(conditionProperty);
204                 result.add(conditionProperty.get("moduleName"));
205             }
206         );
207         return result;
208     }
209
210     private Map<String, String> getPublicPropertyPairs(final List<ConditionProperties> conditionProperties) {
211         final Map<String, String> result = new HashMap<>();
212         getConditions(conditionProperties, PROPERTY_QUERY_NAME).forEach(result::putAll);
213         return result;
214     }
215
216     private Set<NcmpServiceCmHandle> getAllCmHandles() {
217         return queryDataNodes("/dmi-registry/cm-handles").stream()
218             .map(this::createNcmpServiceCmHandle).collect(Collectors.toSet());
219     }
220
221     private Set<String> getAllCmHandleIds() {
222         return cpsAdminPersistenceService.getAnchors("NFP-Operational")
223             .parallelStream().map(Anchor::getName).collect(Collectors.toSet());
224     }
225
226     private List<DataNode> queryDataNodes(final String cmHandlePath) {
227         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
228             cmHandlePath, INCLUDE_ALL_DESCENDANTS);
229     }
230
231     private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) {
232         return convertYangModelCmHandleToNcmpServiceCmHandle(YangDataConverter
233             .convertCmHandleToYangModel(dataNode, dataNode.getLeaves().get("id").toString()));
234     }
235 }