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.inventory;
23 import static org.onap.cps.ncmp.api.impl.utils.YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle;
24 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.stream.Collectors;
32 import lombok.RequiredArgsConstructor;
33 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
34 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
35 import org.onap.cps.spi.CpsDataPersistenceService;
36 import org.onap.cps.spi.FetchDescendantsOption;
37 import org.onap.cps.spi.model.DataNode;
38 import org.springframework.stereotype.Component;
40 @RequiredArgsConstructor
42 public class CmHandleQueries {
44 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
45 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
47 private final CpsDataPersistenceService cpsDataPersistenceService;
48 private static final Map<String, NcmpServiceCmHandle> NO_QUERY_TO_EXECUTE = null;
49 private static final String ANCESTOR_CM_HANDLES = "/ancestor::cm-handles";
53 * Query CmHandles based on PublicProperties.
55 * @param publicPropertyQueryPairs public properties for query
56 * @return CmHandles which have these public properties
58 public Map<String, NcmpServiceCmHandle> queryCmHandlePublicProperties(
59 final Map<String, String> publicPropertyQueryPairs) {
60 if (publicPropertyQueryPairs.isEmpty()) {
61 return Collections.emptyMap();
63 Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandles = null;
64 for (final Map.Entry<String, String> publicPropertyQueryPair : publicPropertyQueryPairs.entrySet()) {
65 final String cpsPath = "//public-properties[@name=\"" + publicPropertyQueryPair.getKey()
66 + "\" and @value=\"" + publicPropertyQueryPair.getValue() + "\"]";
68 final Collection<DataNode> dataNodes = getCmHandleDataNodesByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS);
69 if (cmHandleIdToNcmpServiceCmHandles == null) {
70 cmHandleIdToNcmpServiceCmHandles = collectDataNodesToNcmpServiceCmHandles(dataNodes);
72 final Collection<String> cmHandleIdsToRetain = dataNodes.parallelStream()
73 .map(dataNode -> dataNode.getLeaves().get("id").toString()).collect(Collectors.toSet());
74 cmHandleIdToNcmpServiceCmHandles.keySet().retainAll(cmHandleIdsToRetain);
76 if (cmHandleIdToNcmpServiceCmHandles.isEmpty()) {
80 return cmHandleIdToNcmpServiceCmHandles;
84 * Combine Maps of CmHandles.
86 * @param firstQuery first CmHandles Map
87 * @param secondQuery second CmHandles Map
88 * @return combined Map of CmHandles
90 public Map<String, NcmpServiceCmHandle> combineCmHandleQueries(
91 final Map<String, NcmpServiceCmHandle> firstQuery,
92 final Map<String, NcmpServiceCmHandle> secondQuery) {
93 if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) {
94 return Collections.emptyMap();
95 } else if (firstQuery == NO_QUERY_TO_EXECUTE) {
97 } else if (secondQuery == NO_QUERY_TO_EXECUTE) {
100 firstQuery.keySet().retainAll(secondQuery.keySet());
106 * Method which returns cm handles by the cm handles state.
108 * @param cmHandleState cm handle state
109 * @return a list of cm handles
111 public List<DataNode> getCmHandlesByState(final CmHandleState cmHandleState) {
112 return getCmHandleDataNodesByCpsPath("//state[@cm-handle-state=\"" + cmHandleState + "\"]",
113 INCLUDE_ALL_DESCENDANTS);
117 * Method to return data nodes representing the cm handles.
119 * @param cpsPath cps path for which the cmHandle is requested
120 * @return a list of data nodes representing the cm handles.
122 public List<DataNode> getCmHandleDataNodesByCpsPath(final String cpsPath,
123 final FetchDescendantsOption fetchDescendantsOption) {
124 return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
125 cpsPath + ANCESTOR_CM_HANDLES, fetchDescendantsOption);
129 * Method which returns cm handles by the cm handle id and state.
130 * @param cmHandleId cm handle id
131 * @param cmHandleState cm handle state
132 * @return a list of cm handles
134 public List<DataNode> getCmHandlesByIdAndState(final String cmHandleId, final CmHandleState cmHandleState) {
135 return getCmHandleDataNodesByCpsPath("//cm-handles[@id='" + cmHandleId + "']/state[@cm-handle-state=\""
136 + cmHandleState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS);
140 * Method which returns cm handles by the operational sync state of cm handle.
141 * @param dataStoreSyncState sync state
142 * @return a list of cm handles
144 public List<DataNode> getCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) {
145 return getCmHandleDataNodesByCpsPath("//state/datastores" + "/operational[@sync-state=\""
146 + dataStoreSyncState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS);
149 private Map<String, NcmpServiceCmHandle> collectDataNodesToNcmpServiceCmHandles(
150 final Collection<DataNode> dataNodes) {
151 final Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandle = new HashMap<>();
152 dataNodes.forEach(dataNode -> {
153 final NcmpServiceCmHandle ncmpServiceCmHandle = createNcmpServiceCmHandle(dataNode);
154 cmHandleIdToNcmpServiceCmHandle.put(ncmpServiceCmHandle.getCmHandleId(), ncmpServiceCmHandle);
156 return cmHandleIdToNcmpServiceCmHandle;
159 private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) {
160 return convertYangModelCmHandleToNcmpServiceCmHandle(YangDataConverter
161 .convertCmHandleToYangModel(dataNode, dataNode.getLeaves().get("id").toString()));