2  *  ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2022-2025 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.impl.inventory;
 
  23 import static org.onap.cps.api.parameters.FetchDescendantsOption.DIRECT_CHILDREN_ONLY;
 
  24 import static org.onap.cps.api.parameters.FetchDescendantsOption.OMIT_DESCENDANTS;
 
  25 import static org.onap.cps.ncmp.impl.inventory.CmHandleQueryParametersValidator.validateCpsPathConditionProperties;
 
  26 import static org.onap.cps.ncmp.impl.inventory.CmHandleQueryParametersValidator.validateModuleNameConditionProperties;
 
  27 import static org.onap.cps.ncmp.impl.inventory.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT;
 
  28 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.HAS_ALL_MODULES;
 
  29 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.HAS_ALL_PROPERTIES;
 
  30 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.WITH_CPS_PATH;
 
  31 import static org.onap.cps.ncmp.impl.inventory.models.CmHandleQueryConditions.WITH_TRUST_LEVEL;
 
  32 import static org.onap.cps.ncmp.impl.utils.YangDataConverter.toNcmpServiceCmHandle;
 
  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.BiFunction;
 
  41 import java.util.stream.Collectors;
 
  42 import lombok.RequiredArgsConstructor;
 
  43 import org.onap.cps.api.exceptions.DataValidationException;
 
  44 import org.onap.cps.api.model.ConditionProperties;
 
  45 import org.onap.cps.api.model.DataNode;
 
  46 import org.onap.cps.cpspath.parser.PathParsingException;
 
  47 import org.onap.cps.ncmp.api.inventory.models.CmHandleQueryServiceParameters;
 
  48 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle;
 
  49 import org.onap.cps.ncmp.impl.inventory.models.InventoryQueryConditions;
 
  50 import org.onap.cps.ncmp.impl.inventory.models.PropertyType;
 
  51 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle;
 
  52 import org.onap.cps.ncmp.impl.utils.YangDataConverter;
 
  53 import org.springframework.stereotype.Service;
 
  56 @RequiredArgsConstructor
 
  57 public class ParameterizedCmHandleQueryServiceImpl implements ParameterizedCmHandleQueryService {
 
  59     private static final Collection<String> NO_QUERY_TO_EXECUTE = null;
 
  60     private final CmHandleQueryService cmHandleQueryService;
 
  61     private final InventoryPersistence inventoryPersistence;
 
  64     public Collection<String> queryCmHandleReferenceIds(
 
  65             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
 
  66             final boolean outputAlternateId) {
 
  67         return executeQueries(cmHandleQueryServiceParameters, outputAlternateId,
 
  68                 this::executeCpsPathQuery,
 
  69                 this::queryCmHandlesByPublicProperties,
 
  70                 this::executeModuleNameQuery,
 
  71                 this::queryCmHandlesByTrustLevel);
 
  75     public Collection<String> queryCmHandleIdsForInventory(
 
  76             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
 
  77             final boolean outputAlternateId) {
 
  78         return executeQueries(cmHandleQueryServiceParameters, outputAlternateId,
 
  79                 this::executeCpsPathQuery,
 
  80                 this::queryCmHandlesByPublicProperties,
 
  81                 this::queryCmHandlesByPrivateProperties,
 
  82                 this::queryCmHandlesByDmiPlugin);
 
  86     public Collection<NcmpServiceCmHandle> queryCmHandles(
 
  87             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
 
  89         if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
 
  90             return getAllCmHandles();
 
  93         final Collection<String> cmHandleIds = queryCmHandleReferenceIds(cmHandleQueryServiceParameters, false);
 
  95         return getNcmpServiceCmHandles(cmHandleIds);
 
  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());
 
 104     private Collection<String> queryCmHandlesByDmiPlugin(
 
 105             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final boolean outputAlternateId) {
 
 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;
 
 113         final String dmiPluginIdentifierValue = dmiPropertyQueryPairs
 
 114                 .get(PropertyType.DMI_PLUGIN.getYangContainerName());
 
 116         return cmHandleQueryService.getCmHandleReferencesByDmiPluginIdentifier(
 
 117                 dmiPluginIdentifierValue, outputAlternateId);
 
 121     private Collection<String> queryCmHandlesByPrivateProperties(
 
 122             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final boolean outputAlternateId) {
 
 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 cmHandleQueryService.queryCmHandleAdditionalProperties(privatePropertyQueryPairs, outputAlternateId);
 
 134     private Collection<String> queryCmHandlesByPublicProperties(
 
 135             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final boolean outputAlternateId) {
 
 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 cmHandleQueryService.queryCmHandlePublicProperties(publicPropertyQueryPairs, outputAlternateId);
 
 147     private Collection<String> queryCmHandlesByTrustLevel(final CmHandleQueryServiceParameters
 
 148                                                                   cmHandleQueryServiceParameters,
 
 149                                                           final boolean outputAlternateId) {
 
 151         final Map<String, String> trustLevelPropertyQueryPairs =
 
 152                 getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(),
 
 153                         WITH_TRUST_LEVEL.getConditionName());
 
 155         if (trustLevelPropertyQueryPairs.isEmpty()) {
 
 156             return NO_QUERY_TO_EXECUTE;
 
 158         return cmHandleQueryService.queryCmHandlesByTrustLevel(trustLevelPropertyQueryPairs, outputAlternateId);
 
 161     private Collection<String> executeModuleNameQuery(
 
 162             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final boolean outputAlternateId) {
 
 163         final Collection<String> moduleNamesForQuery =
 
 164                 getModuleNamesForQuery(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
 
 165         if (moduleNamesForQuery.isEmpty()) {
 
 166             return NO_QUERY_TO_EXECUTE;
 
 168         return inventoryPersistence.getCmHandleReferencesWithGivenModules(moduleNamesForQuery, outputAlternateId);
 
 171     private Collection<String> executeCpsPathQuery(
 
 172             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final boolean outputAlternateId) {
 
 173         final Map<String, String> cpsPathCondition
 
 174                 = getCpsPathCondition(cmHandleQueryServiceParameters.getCmHandleQueryParameters());
 
 175         if (!validateCpsPathConditionProperties(cpsPathCondition)) {
 
 176             return Collections.emptySet();
 
 178         final Collection<String> cpsPathQueryResult;
 
 179         if (cpsPathCondition.isEmpty()) {
 
 180             return NO_QUERY_TO_EXECUTE;
 
 183             cpsPathQueryResult = collectCmHandleReferencesFromDataNodes(
 
 184                     cmHandleQueryService.queryCmHandleAncestorsByCpsPath(cpsPathCondition.get("cpsPath"),
 
 185                             OMIT_DESCENDANTS), outputAlternateId);
 
 186         } catch (final PathParsingException pathParsingException) {
 
 187             throw new DataValidationException(pathParsingException.getMessage(), pathParsingException.getDetails(),
 
 188                     pathParsingException);
 
 190         return cpsPathQueryResult;
 
 193     private Collection<String> getModuleNamesForQuery(final List<ConditionProperties> conditionProperties) {
 
 194         final List<String> result = new ArrayList<>();
 
 195         getConditions(conditionProperties, HAS_ALL_MODULES.getConditionName()).forEach(
 
 196                 conditionProperty -> {
 
 197                     validateModuleNameConditionProperties(conditionProperty);
 
 198                     result.add(conditionProperty.get("moduleName"));
 
 203     private Map<String, String> getCpsPathCondition(final List<ConditionProperties> conditionProperties) {
 
 204         final Map<String, String> result = new HashMap<>();
 
 205         getConditions(conditionProperties, WITH_CPS_PATH.getConditionName()).forEach(result::putAll);
 
 209     private Map<String, String> getPropertyPairs(final List<ConditionProperties> conditionProperties,
 
 210                                                  final String queryProperty) {
 
 211         final Map<String, String> result = new HashMap<>();
 
 212         getConditions(conditionProperties, queryProperty).forEach(result::putAll);
 
 216     private List<Map<String, String>> getConditions(final List<ConditionProperties> conditionProperties,
 
 218         for (final ConditionProperties conditionProperty : conditionProperties) {
 
 219             if (conditionProperty.getConditionName().equals(name)) {
 
 220                 return conditionProperty.getConditionParameters();
 
 223         return Collections.emptyList();
 
 226     private Collection<String> getAllCmHandleReferences(final boolean outputAlternateId) {
 
 227         final DataNode dataNode = inventoryPersistence.getDataNode(NCMP_DMI_REGISTRY_PARENT, DIRECT_CHILDREN_ONLY)
 
 229         return collectCmHandleReferencesFromDataNodes(dataNode.getChildDataNodes(), outputAlternateId);
 
 232     private Collection<NcmpServiceCmHandle> getNcmpServiceCmHandles(final Collection<String> cmHandleIds) {
 
 233         final Collection<YangModelCmHandle> yangModelcmHandles
 
 234                 = inventoryPersistence.getYangModelCmHandles(cmHandleIds);
 
 236         final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles = new ArrayList<>(yangModelcmHandles.size());
 
 238         yangModelcmHandles.forEach(yangModelcmHandle ->
 
 239                 ncmpServiceCmHandles.add(YangDataConverter.toNcmpServiceCmHandle(yangModelcmHandle))
 
 241         return ncmpServiceCmHandles;
 
 244     private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) {
 
 245         return toNcmpServiceCmHandle(YangDataConverter.toYangModelCmHandle(dataNode));
 
 248     private Collection<String> executeQueries(final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
 
 249                                               final boolean outputAlternateId,
 
 250                                               final BiFunction<CmHandleQueryServiceParameters, Boolean,
 
 251                                                       Collection<String>>... queryFunctions) {
 
 252         if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
 
 253             return getAllCmHandleReferences(outputAlternateId);
 
 255         Collection<String> combinedQueryResult = NO_QUERY_TO_EXECUTE;
 
 256         for (final BiFunction<CmHandleQueryServiceParameters, Boolean,
 
 257                 Collection<String>> queryFunction : queryFunctions) {
 
 258             final Collection<String> queryResult = queryFunction.apply(cmHandleQueryServiceParameters,
 
 260             if (noEntriesFoundCanStopQuerying(queryResult)) {
 
 261                 return Collections.emptySet();
 
 263             combinedQueryResult = combineCmHandleQueryResults(combinedQueryResult, queryResult);
 
 265         return combinedQueryResult;
 
 268     private boolean noEntriesFoundCanStopQuerying(final Collection<String> queryResult) {
 
 269         return queryResult != NO_QUERY_TO_EXECUTE && queryResult.isEmpty();
 
 272     private Collection<String> combineCmHandleQueryResults(final Collection<String> firstQuery,
 
 273                                                            final Collection<String> secondQuery) {
 
 274         if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) {
 
 275             return NO_QUERY_TO_EXECUTE;
 
 276         } else if (firstQuery == NO_QUERY_TO_EXECUTE) {
 
 278         } else if (secondQuery == NO_QUERY_TO_EXECUTE) {
 
 281             firstQuery.retainAll(secondQuery);
 
 286     private Collection<String> collectCmHandleReferencesFromDataNodes(final Collection<DataNode> dataNodes,
 
 287                                                                       final boolean outputAlternateId) {
 
 288         if (outputAlternateId) {
 
 289             return dataNodes.stream().map(dataNode ->
 
 290                     (String) dataNode.getLeaves().get("alternate-id")).collect(Collectors.toSet());
 
 292             return dataNodes.stream().map(dataNode ->
 
 293                     (String) dataNode.getLeaves().get("id")).collect(Collectors.toSet());