Merge "CM SUBSCRIPTION: add new subscription for non existing xpath"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / NetworkCmProxyCmHandleQueryServiceImpl.java
1 /*
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
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.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;
33
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;
39 import java.util.Map;
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;
58
59 @Service
60 @Slf4j
61 @RequiredArgsConstructor
62 public class NetworkCmProxyCmHandleQueryServiceImpl implements NetworkCmProxyCmHandleQueryService {
63
64     private static final Collection<String> NO_QUERY_TO_EXECUTE = null;
65     private final CmHandleQueries cmHandleQueries;
66     private final InventoryPersistence inventoryPersistence;
67
68     @Override
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);
76     }
77
78     @Override
79     public Collection<String> queryCmHandleIdsForInventory(
80         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
81         return executeQueries(cmHandleQueryServiceParameters,
82             this::queryCmHandlesByPublicProperties,
83             this::queryCmHandlesByPrivateProperties,
84             this::queryCmHandlesByDmiPlugin);
85     }
86
87     @Override
88     public Collection<NcmpServiceCmHandle> queryCmHandles(
89         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
90
91         if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
92             return getAllCmHandles();
93         }
94
95         final Collection<String> cmHandleIds = queryCmHandleIds(cmHandleQueryServiceParameters);
96
97         return getNcmpServiceCmHandles(cmHandleIds);
98     }
99
100     @Override
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());
104     }
105
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;
113         }
114
115         final String dmiPluginIdentifierValue = dmiPropertyQueryPairs
116             .get(PropertyType.DMI_PLUGIN.getYangContainerName());
117
118         return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifierValue);
119     }
120
121     private Collection<String> queryCmHandlesByPrivateProperties(
122             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
123
124         final Map<String, String> privatePropertyQueryPairs =
125                 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
126                         InventoryQueryConditions.HAS_ALL_ADDITIONAL_PROPERTIES.getName());
127
128         if (privatePropertyQueryPairs.isEmpty()) {
129             return NO_QUERY_TO_EXECUTE;
130         }
131         return cmHandleQueries.queryCmHandleAdditionalProperties(privatePropertyQueryPairs);
132     }
133
134     private Collection<String> queryCmHandlesByPublicProperties(
135             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
136
137         final Map<String, String> publicPropertyQueryPairs =
138                 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
139                         HAS_ALL_PROPERTIES.getConditionName());
140
141         if (publicPropertyQueryPairs.isEmpty()) {
142             return NO_QUERY_TO_EXECUTE;
143         }
144         return cmHandleQueries.queryCmHandlePublicProperties(publicPropertyQueryPairs);
145     }
146
147     private Collection<String> queryCmHandlesByTrustLevel(final CmHandleQueryServiceParameters
148                                                                   cmHandleQueryServiceParameters) {
149
150         final Map<String, String> trustLevelPropertyQueryPairs =
151                 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
152                         WITH_TRUST_LEVEL.getConditionName());
153
154         if (trustLevelPropertyQueryPairs.isEmpty()) {
155             return NO_QUERY_TO_EXECUTE;
156         }
157         return cmHandleQueries.queryCmHandlesByTrustLevel(trustLevelPropertyQueryPairs);
158     }
159
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;
166         }
167         return inventoryPersistence.getCmHandleIdsWithGivenModules(moduleNamesForQuery);
168     }
169
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();
176         }
177         final Collection<String> cpsPathQueryResult;
178         if (cpsPathCondition.isEmpty()) {
179             return NO_QUERY_TO_EXECUTE;
180         }
181         try {
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);
188         }
189         return cpsPathQueryResult;
190     }
191
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"));
198                 });
199         return result;
200     }
201
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);
205         return result;
206     }
207
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);
212         return result;
213     }
214
215     private List<Map<String, String>> getConditions(final List<ConditionProperties> conditionProperties,
216                                                     final String name) {
217         for (final ConditionProperties conditionProperty : conditionProperties) {
218             if (conditionProperty.getConditionName().equals(name)) {
219                 return conditionProperty.getConditionParameters();
220             }
221         }
222         return Collections.emptyList();
223     }
224
225     private Collection<String> getAllCmHandleIds() {
226         final DataNode dataNode = inventoryPersistence.getDataNode(NCMP_DMI_REGISTRY_PARENT, DIRECT_CHILDREN_ONLY)
227                 .iterator().next();
228         return collectCmHandleIdsFromDataNodes(dataNode.getChildDataNodes());
229     }
230
231     private Collection<NcmpServiceCmHandle> getNcmpServiceCmHandles(final Collection<String> cmHandleIds) {
232         final Collection<YangModelCmHandle> yangModelcmHandles
233             = inventoryPersistence.getYangModelCmHandles(cmHandleIds);
234
235         final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles = new ArrayList<>(yangModelcmHandles.size());
236
237         yangModelcmHandles.forEach(yangModelcmHandle ->
238             ncmpServiceCmHandles.add(YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(yangModelcmHandle))
239         );
240         return ncmpServiceCmHandles;
241     }
242
243     private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) {
244         return convertYangModelCmHandleToNcmpServiceCmHandle(YangDataConverter.convertCmHandleToYangModel(dataNode));
245     }
246
247     private Collection<String> executeQueries(final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
248                                               final Function<CmHandleQueryServiceParameters, Collection<String>>...
249                                                   queryFunctions) {
250         if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
251             return getAllCmHandleIds();
252         }
253         Collection<String> combinedQueryResult = NO_QUERY_TO_EXECUTE;
254         for (final Function<CmHandleQueryServiceParameters, Collection<String>> queryFunction : queryFunctions) {
255             final Collection<String> queryResult = queryFunction.apply(cmHandleQueryServiceParameters);
256             if (noEntriesFoundCanStopQuerying(queryResult)) {
257                 return Collections.emptySet();
258             }
259             combinedQueryResult = combineCmHandleQueryResults(combinedQueryResult, queryResult);
260         }
261         return combinedQueryResult;
262     }
263
264     private boolean noEntriesFoundCanStopQuerying(final Collection<String> queryResult) {
265         return queryResult != NO_QUERY_TO_EXECUTE && queryResult.isEmpty();
266     }
267
268     private Collection<String> combineCmHandleQueryResults(final Collection<String> firstQuery,
269                                                            final Collection<String> secondQuery) {
270         if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) {
271             return NO_QUERY_TO_EXECUTE;
272         } else if (firstQuery == NO_QUERY_TO_EXECUTE) {
273             return secondQuery;
274         } else if (secondQuery == NO_QUERY_TO_EXECUTE) {
275             return firstQuery;
276         } else {
277             firstQuery.retainAll(secondQuery);
278             return firstQuery;
279         }
280     }
281
282     private Collection<String> collectCmHandleIdsFromDataNodes(final Collection<DataNode> dataNodes) {
283         return dataNodes.stream().map(dataNode -> (String) dataNode.getLeaves().get("id")).collect(Collectors.toSet());
284     }
285
286 }