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.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.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
26 import static org.onap.cps.utils.CmHandleQueryRestParametersValidator.validateModuleNameConditionProperties;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.HashMap;
32 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.spi.CpsAdminPersistenceService;
39 import org.onap.cps.spi.CpsDataPersistenceService;
40 import org.onap.cps.spi.model.Anchor;
41 import org.onap.cps.spi.model.CmHandleQueryServiceParameters;
42 import org.onap.cps.spi.model.ConditionProperties;
43 import org.onap.cps.spi.model.DataNode;
44 import org.onap.cps.spi.model.DataNodeIdentifier;
45 import org.onap.cps.utils.JsonObjectMapper;
46 import org.springframework.stereotype.Service;
50 @RequiredArgsConstructor
51 public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCmHandlerQueryService {
53 private static final String PROPERTY_QUERY_NAME = "hasAllProperties";
54 private static final String MODULE_QUERY_NAME = "hasAllModules";
55 private final CpsDataPersistenceService cpsDataPersistenceService;
56 private final CpsAdminPersistenceService cpsAdminPersistenceService;
57 private final JsonObjectMapper jsonObjectMapper;
60 * Query and return cm handles that match the given query parameters.
62 * @param cmHandleQueryServiceParameters the cm handle query parameters
63 * @return collection of cm handles
66 public Collection<DataNode> queryCmHandles(final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
68 if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) {
69 return getAllCmHandles();
72 final Collection<DataNodeIdentifier> amalgamatedQueryResultIdentifiers = new ArrayList<>();
73 final Map<DataNodeIdentifier, DataNode> amalgamatedQueryResults = new HashMap<>();
75 final boolean firstQuery = moduleNameQuery(cmHandleQueryServiceParameters,
76 amalgamatedQueryResultIdentifiers, amalgamatedQueryResults);
78 publicPropertyQuery(cmHandleQueryServiceParameters, amalgamatedQueryResultIdentifiers,
79 amalgamatedQueryResults, firstQuery);
81 final Collection<DataNode> filteredDataNodes = new ArrayList<>();
82 amalgamatedQueryResultIdentifiers.forEach(amalgamatedQueryResultIdentifier ->
83 filteredDataNodes.add(amalgamatedQueryResults.get(amalgamatedQueryResultIdentifier))
86 return filteredDataNodes;
89 private void publicPropertyQuery(final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
90 final Collection<DataNodeIdentifier> amalgamatedQueryResultIdentifiers,
91 final Map<DataNodeIdentifier, DataNode> amalgamatedQueryResults,
93 for (final Map.Entry<String, String> entry :
94 getPublicPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters()).entrySet()) {
95 final String cmHandlePath = "//public-properties[@name='" + entry.getKey() + "' " + "and @value='"
96 + entry.getValue() + "']" + "/ancestor::cm-handles";
98 final Collection<DataNode> dataNodes = getDataNodes(cmHandlePath);
102 dataNodes.forEach(dataNode -> {
103 final DataNodeIdentifier dataNodeIdentifier =
104 jsonObjectMapper.convertToValueType(dataNode, DataNodeIdentifier.class);
105 amalgamatedQueryResultIdentifiers.add(dataNodeIdentifier);
106 amalgamatedQueryResults.put(dataNodeIdentifier, dataNode);
109 final Collection<DataNodeIdentifier> singleConditionQueryDataNodeIdentifiers = new ArrayList<>();
110 dataNodes.forEach(dataNode -> {
111 final DataNodeIdentifier dataNodeIdentifier =
112 jsonObjectMapper.convertToValueType(dataNode, DataNodeIdentifier.class);
113 singleConditionQueryDataNodeIdentifiers.add(dataNodeIdentifier);
114 amalgamatedQueryResults.put(dataNodeIdentifier, dataNode);
116 amalgamatedQueryResultIdentifiers.retainAll(singleConditionQueryDataNodeIdentifiers);
119 if (amalgamatedQueryResultIdentifiers.isEmpty()) {
125 private boolean moduleNameQuery(final CmHandleQueryServiceParameters cmHandleQueryServiceParameters,
126 final Collection<DataNodeIdentifier> amalgamatedQueryResultIdentifiers,
127 final Map<DataNodeIdentifier, DataNode> amalgamatedQueryResults) {
128 boolean firstQuery = true;
129 if (!getModuleNames(cmHandleQueryServiceParameters.getCmHandleQueryParameters()).isEmpty()) {
130 final Collection<String> anchorNames = cpsAdminPersistenceService.queryAnchors("NFP-Operational",
131 getModuleNames(cmHandleQueryServiceParameters.getCmHandleQueryParameters()))
132 .parallelStream().map(Anchor::getName).collect(Collectors.toList());
134 getAllCmHandles().forEach(dataNode -> {
135 if (anchorNames.contains(dataNode.getLeaves().get("id").toString())) {
136 final DataNodeIdentifier dataNodeIdentifier =
137 jsonObjectMapper.convertToValueType(dataNode, DataNodeIdentifier.class);
138 amalgamatedQueryResultIdentifiers.add(dataNodeIdentifier);
139 amalgamatedQueryResults.put(dataNodeIdentifier, dataNode);
148 private List<Map<String, String>> getConditions(final List<ConditionProperties> conditionProperties,
150 for (final ConditionProperties conditionProperty : conditionProperties) {
151 if (conditionProperty.getConditionName().equals(name)) {
152 return conditionProperty.getConditionParameters();
155 return Collections.emptyList();
158 private List<String> getModuleNames(final List<ConditionProperties> conditionProperties) {
159 final List<String> result = new ArrayList<>();
160 getConditions(conditionProperties, MODULE_QUERY_NAME).parallelStream().forEach(
161 conditionProperty -> {
162 validateModuleNameConditionProperties(conditionProperty);
163 result.add(conditionProperty.get("moduleName"));
169 private Map<String, String> getPublicPropertyPairs(final List<ConditionProperties> conditionProperties) {
170 final Map<String, String> result = new HashMap<>();
171 getConditions(conditionProperties, PROPERTY_QUERY_NAME).forEach(result::putAll);
175 private Collection<DataNode> getAllCmHandles() {
176 return getDataNodes("//public-properties/ancestor::cm-handles");
179 private List<DataNode> getDataNodes(final String cmHandlePath) {
180 return cpsDataPersistenceService.queryDataNodes(
181 NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, cmHandlePath, INCLUDE_ALL_DESCENDANTS);