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;
25 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.stream.Collectors;
33 import lombok.RequiredArgsConstructor;
34 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
35 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
36 import org.onap.cps.spi.CpsDataPersistenceService;
37 import org.onap.cps.spi.FetchDescendantsOption;
38 import org.onap.cps.spi.model.DataNode;
39 import org.springframework.stereotype.Component;
41 @RequiredArgsConstructor
43 public class CmHandleQueries {
45 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
46 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
48 private final CpsDataPersistenceService cpsDataPersistenceService;
49 private static final Map<String, NcmpServiceCmHandle> NO_QUERY_TO_EXECUTE = null;
50 private static final String ANCESTOR_CM_HANDLES = "/ancestor::cm-handles";
54 * Query CmHandles based on PublicProperties.
56 * @param publicPropertyQueryPairs public properties for query
57 * @return CmHandles which have these public properties
59 public Map<String, NcmpServiceCmHandle> queryCmHandlePublicProperties(
60 final Map<String, String> publicPropertyQueryPairs) {
61 if (publicPropertyQueryPairs.isEmpty()) {
62 return Collections.emptyMap();
64 Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandles = null;
65 for (final Map.Entry<String, String> publicPropertyQueryPair : publicPropertyQueryPairs.entrySet()) {
66 final String cpsPath = "//public-properties[@name=\"" + publicPropertyQueryPair.getKey()
67 + "\" and @value=\"" + publicPropertyQueryPair.getValue() + "\"]";
69 final Collection<DataNode> dataNodes = queryCmHandleDataNodesByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS);
70 if (cmHandleIdToNcmpServiceCmHandles == null) {
71 cmHandleIdToNcmpServiceCmHandles = collectDataNodesToNcmpServiceCmHandles(dataNodes);
73 final Collection<String> cmHandleIdsToRetain = dataNodes.parallelStream()
74 .map(dataNode -> dataNode.getLeaves().get("id").toString()).collect(Collectors.toSet());
75 cmHandleIdToNcmpServiceCmHandles.keySet().retainAll(cmHandleIdsToRetain);
77 if (cmHandleIdToNcmpServiceCmHandles.isEmpty()) {
81 return cmHandleIdToNcmpServiceCmHandles;
85 * Combine Maps of CmHandles.
87 * @param firstQuery first CmHandles Map
88 * @param secondQuery second CmHandles Map
89 * @return combined Map of CmHandles
91 public Map<String, NcmpServiceCmHandle> combineCmHandleQueries(
92 final Map<String, NcmpServiceCmHandle> firstQuery,
93 final Map<String, NcmpServiceCmHandle> secondQuery) {
94 if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) {
95 return Collections.emptyMap();
96 } else if (firstQuery == NO_QUERY_TO_EXECUTE) {
98 } else if (secondQuery == NO_QUERY_TO_EXECUTE) {
101 firstQuery.keySet().retainAll(secondQuery.keySet());
107 * Method which returns cm handles by the cm handles state.
109 * @param cmHandleState cm handle state
110 * @return a list of cm handles
112 public List<DataNode> queryCmHandlesByState(final CmHandleState cmHandleState) {
113 return queryCmHandleDataNodesByCpsPath("//state[@cm-handle-state=\"" + cmHandleState + "\"]",
114 INCLUDE_ALL_DESCENDANTS);
118 * Method to return data nodes representing the cm handles.
120 * @param cpsPath cps path for which the cmHandle is requested
121 * @return a list of data nodes representing the cm handles.
123 public List<DataNode> queryCmHandleDataNodesByCpsPath(final String cpsPath,
124 final FetchDescendantsOption fetchDescendantsOption) {
125 return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
126 cpsPath + ANCESTOR_CM_HANDLES, fetchDescendantsOption);
130 * Method to check the state of a cm handle with given id.
132 * @param cmHandleId cm handle id
133 * @param requiredCmHandleState the required state of the cm handle
134 * @return a boolean, true if the state is equal to the required state
136 public boolean cmHandleHasState(final String cmHandleId, final CmHandleState requiredCmHandleState) {
137 final DataNode stateDataNode = getCmHandleState(cmHandleId);
138 final String cmHandleStateAsString = (String) stateDataNode.getLeaves().get("cm-handle-state");
139 return CmHandleState.valueOf(cmHandleStateAsString).equals(requiredCmHandleState);
143 * Method which returns cm handles by the operational sync state of cm handle.
144 * @param dataStoreSyncState sync state
145 * @return a list of cm handles
147 public List<DataNode> queryCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) {
148 return queryCmHandleDataNodesByCpsPath("//state/datastores" + "/operational[@sync-state=\""
149 + dataStoreSyncState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS);
152 private Map<String, NcmpServiceCmHandle> collectDataNodesToNcmpServiceCmHandles(
153 final Collection<DataNode> dataNodes) {
154 final Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandle = new HashMap<>();
155 dataNodes.forEach(dataNode -> {
156 final NcmpServiceCmHandle ncmpServiceCmHandle = createNcmpServiceCmHandle(dataNode);
157 cmHandleIdToNcmpServiceCmHandle.put(ncmpServiceCmHandle.getCmHandleId(), ncmpServiceCmHandle);
159 return cmHandleIdToNcmpServiceCmHandle;
162 private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) {
163 return convertYangModelCmHandleToNcmpServiceCmHandle(YangDataConverter
164 .convertCmHandleToYangModel(dataNode, dataNode.getLeaves().get("id").toString()));
167 private DataNode getCmHandleState(final String cmHandleId) {
168 final String xpath = "/dmi-registry/cm-handles[@id='" + cmHandleId + "']/state";
169 return cpsDataPersistenceService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
170 xpath, OMIT_DESCENDANTS);