34eeaccf8f178f4a486e89814172cc731b230158
[cps.git] /
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.impl.inventory;
22
23 import static org.onap.cps.ncmp.impl.inventory.CmHandleQueryParametersValidator.validateCpsPathConditionProperties;
24 import static org.onap.cps.ncmp.impl.inventory.CmHandleQueryParametersValidator.validateModuleNameConditionProperties;
25 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT;
26 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.HAS_ALL_MODULES;
27 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.HAS_ALL_PROPERTIES;
28 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.WITH_CPS_PATH;
29 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.WITH_TRUST_LEVEL;
30 import static org.onap.cps.ncmp.impl.utils.YangDataConverter.toNcmpServiceCmHandle;
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.inventory.models.CmHandleQueryServiceParameters;
46 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle;
47 import org.onap.cps.ncmp.impl.inventory.models.InventoryQueryConditions;
48 import org.onap.cps.ncmp.impl.inventory.models.PropertyType;
49 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle;
50 import org.onap.cps.ncmp.impl.utils.YangDataConverter;
51 import org.onap.cps.spi.exceptions.DataValidationException;
52 import org.onap.cps.spi.model.ConditionProperties;
53 import org.onap.cps.spi.model.DataNode;
54 import org.springframework.stereotype.Service;
55
56 @Service
57 @Slf4j
58 @RequiredArgsConstructor
59 public class ParameterizedCmHandleQueryServiceImpl implements ParameterizedCmHandleQueryService {
60
61     private static final Collection<String> NO_QUERY_TO_EXECUTE = null;
62     private final CmHandleQueryService cmHandleQueryService;
63     private final InventoryPersistence inventoryPersistence;
64
65     @Override
66     public Collection<String> queryCmHandleIds(
67             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
68         return executeQueries(cmHandleQueryServiceParameters,
69                 this::executeCpsPathQuery,
70                 this::queryCmHandlesByPublicProperties,
71                 this::executeModuleNameQuery,
72                 this::queryCmHandlesByTrustLevel);
73     }
74
75     @Override
76     public Collection<String> queryCmHandleIdsForInventory(
77             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
78         return executeQueries(cmHandleQueryServiceParameters,
79                 this::executeCpsPathQuery,
80                 this::queryCmHandlesByPublicProperties,
81                 this::queryCmHandlesByPrivateProperties,
82                 this::queryCmHandlesByDmiPlugin);
83     }
84
85     @Override
86     public Collection<NcmpServiceCmHandle> queryCmHandles(
87         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
88
89         if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
90             return getAllCmHandles();
91         }
92
93         final Collection<String> cmHandleIds = queryCmHandleIds(cmHandleQueryServiceParameters);
94
95         return getNcmpServiceCmHandles(cmHandleIds);
96     }
97
98     @Override
99     public Collection<NcmpServiceCmHandle> getAllCmHandles() {
100         final DataNode dataNode = inventoryPersistence.getDataNode(NCMP_DMI_REGISTRY_PARENT).iterator().next();
101         return dataNode.getChildDataNodes().stream().map(this::createNcmpServiceCmHandle).collect(Collectors.toSet());
102     }
103
104     private Collection<String> queryCmHandlesByDmiPlugin(
105             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
106         final Map<String, String> dmiPropertyQueryPairs =
107                 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
108                         InventoryQueryConditions.CM_HANDLE_WITH_DMI_PLUGIN.getName());
109         if (dmiPropertyQueryPairs.isEmpty()) {
110             return NO_QUERY_TO_EXECUTE;
111         }
112
113         final String dmiPluginIdentifierValue = dmiPropertyQueryPairs
114             .get(PropertyType.DMI_PLUGIN.getYangContainerName());
115
116         return cmHandleQueryService.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifierValue);
117     }
118
119     private Collection<String> queryCmHandlesByPrivateProperties(
120             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
121
122         final Map<String, String> privatePropertyQueryPairs =
123                 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
124                         InventoryQueryConditions.HAS_ALL_ADDITIONAL_PROPERTIES.getName());
125
126         if (privatePropertyQueryPairs.isEmpty()) {
127             return NO_QUERY_TO_EXECUTE;
128         }
129         return cmHandleQueryService.queryCmHandleAdditionalProperties(privatePropertyQueryPairs);
130     }
131
132     private Collection<String> queryCmHandlesByPublicProperties(
133             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
134
135         final Map<String, String> publicPropertyQueryPairs =
136                 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
137                         HAS_ALL_PROPERTIES.getConditionName());
138
139         if (publicPropertyQueryPairs.isEmpty()) {
140             return NO_QUERY_TO_EXECUTE;
141         }
142         return cmHandleQueryService.queryCmHandlePublicProperties(publicPropertyQueryPairs);
143     }
144
145     private Collection<String> queryCmHandlesByTrustLevel(final CmHandleQueryServiceParameters
146                                                                   cmHandleQueryServiceParameters) {
147
148         final Map<String, String> trustLevelPropertyQueryPairs =
149                 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
150                         WITH_TRUST_LEVEL.getConditionName());
151
152         if (trustLevelPropertyQueryPairs.isEmpty()) {
153             return NO_QUERY_TO_EXECUTE;
154         }
155         return cmHandleQueryService.queryCmHandlesByTrustLevel(trustLevelPropertyQueryPairs);
156     }
157
158     private Collection<String> executeModuleNameQuery(
159             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
160         final Collection<String> moduleNamesForQuery =
161                 getModuleNamesForQuery(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
162         if (moduleNamesForQuery.isEmpty()) {
163             return NO_QUERY_TO_EXECUTE;
164         }
165         return inventoryPersistence.getCmHandleIdsWithGivenModules(moduleNamesForQuery);
166     }
167
168     private Collection<String> executeCpsPathQuery(
169             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
170         final Map<String, String> cpsPathCondition
171             = getCpsPathCondition(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
172         if (!validateCpsPathConditionProperties(cpsPathCondition)) {
173             return Collections.emptySet();
174         }
175         final Collection<String> cpsPathQueryResult;
176         if (cpsPathCondition.isEmpty()) {
177             return NO_QUERY_TO_EXECUTE;
178         }
179         try {
180             cpsPathQueryResult = collectCmHandleIdsFromDataNodes(
181                 cmHandleQueryService.queryCmHandleAncestorsByCpsPath(
182                         cpsPathCondition.get("cpsPath"), OMIT_DESCENDANTS));
183         } catch (final PathParsingException pathParsingException) {
184             throw new DataValidationException(pathParsingException.getMessage(), pathParsingException.getDetails(),
185                     pathParsingException);
186         }
187         return cpsPathQueryResult;
188     }
189
190     private Collection<String> getModuleNamesForQuery(final List<ConditionProperties> conditionProperties) {
191         final List<String> result = new ArrayList<>();
192         getConditions(conditionProperties, HAS_ALL_MODULES.getConditionName()).forEach(
193                 conditionProperty -> {
194                     validateModuleNameConditionProperties(conditionProperty);
195                     result.add(conditionProperty.get("moduleName"));
196                 });
197         return result;
198     }
199
200     private Map<String, String> getCpsPathCondition(final List<ConditionProperties> conditionProperties) {
201         final Map<String, String> result = new HashMap<>();
202         getConditions(conditionProperties, WITH_CPS_PATH.getConditionName()).forEach(result::putAll);
203         return result;
204     }
205
206     private Map<String, String> getPropertyPairs(final List<ConditionProperties> conditionProperties,
207                                                        final String queryProperty) {
208         final Map<String, String> result = new HashMap<>();
209         getConditions(conditionProperties, queryProperty).forEach(result::putAll);
210         return result;
211     }
212
213     private List<Map<String, String>> getConditions(final List<ConditionProperties> conditionProperties,
214                                                     final String name) {
215         for (final ConditionProperties conditionProperty : conditionProperties) {
216             if (conditionProperty.getConditionName().equals(name)) {
217                 return conditionProperty.getConditionParameters();
218             }
219         }
220         return Collections.emptyList();
221     }
222
223     private Collection<String> getAllCmHandleIds() {
224         final DataNode dataNode = inventoryPersistence.getDataNode(NCMP_DMI_REGISTRY_PARENT, DIRECT_CHILDREN_ONLY)
225                 .iterator().next();
226         return collectCmHandleIdsFromDataNodes(dataNode.getChildDataNodes());
227     }
228
229     private Collection<NcmpServiceCmHandle> getNcmpServiceCmHandles(final Collection<String> cmHandleIds) {
230         final Collection<YangModelCmHandle> yangModelcmHandles
231             = inventoryPersistence.getYangModelCmHandles(cmHandleIds);
232
233         final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles = new ArrayList<>(yangModelcmHandles.size());
234
235         yangModelcmHandles.forEach(yangModelcmHandle ->
236             ncmpServiceCmHandles.add(YangDataConverter.toNcmpServiceCmHandle(yangModelcmHandle))
237         );
238         return ncmpServiceCmHandles;
239     }
240
241     private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) {
242         return toNcmpServiceCmHandle(YangDataConverter.toYangModelCmHandle(dataNode));
243     }
244
245     private Collection<String> executeQueries(final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
246                                               final Function<CmHandleQueryServiceParameters, Collection<String>>...
247                                                   queryFunctions) {
248         if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
249             return getAllCmHandleIds();
250         }
251         Collection<String> combinedQueryResult = NO_QUERY_TO_EXECUTE;
252         for (final Function<CmHandleQueryServiceParameters, Collection<String>> queryFunction : queryFunctions) {
253             final Collection<String> queryResult = queryFunction.apply(cmHandleQueryServiceParameters);
254             if (noEntriesFoundCanStopQuerying(queryResult)) {
255                 return Collections.emptySet();
256             }
257             combinedQueryResult = combineCmHandleQueryResults(combinedQueryResult, queryResult);
258         }
259         return combinedQueryResult;
260     }
261
262     private boolean noEntriesFoundCanStopQuerying(final Collection<String> queryResult) {
263         return queryResult != NO_QUERY_TO_EXECUTE && queryResult.isEmpty();
264     }
265
266     private Collection<String> combineCmHandleQueryResults(final Collection<String> firstQuery,
267                                                            final Collection<String> secondQuery) {
268         if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) {
269             return NO_QUERY_TO_EXECUTE;
270         } else if (firstQuery == NO_QUERY_TO_EXECUTE) {
271             return secondQuery;
272         } else if (secondQuery == NO_QUERY_TO_EXECUTE) {
273             return firstQuery;
274         } else {
275             firstQuery.retainAll(secondQuery);
276             return firstQuery;
277         }
278     }
279
280     private Collection<String> collectCmHandleIdsFromDataNodes(final Collection<DataNode> dataNodes) {
281         return dataNodes.stream().map(dataNode -> (String) dataNode.getLeaves().get("id")).collect(Collectors.toSet());
282     }
283
284 }