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.HashSet;
31 import java.util.List;
34 import java.util.stream.Collectors;
35 import lombok.RequiredArgsConstructor;
36 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
37 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
38 import org.onap.cps.spi.CpsDataPersistenceService;
39 import org.onap.cps.spi.FetchDescendantsOption;
40 import org.onap.cps.spi.model.DataNode;
41 import org.springframework.stereotype.Component;
43 @RequiredArgsConstructor
45 public class CmHandleQueriesImpl implements CmHandleQueries {
47 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
48 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
50 private final CpsDataPersistenceService cpsDataPersistenceService;
51 private static final Map<String, NcmpServiceCmHandle> NO_QUERY_TO_EXECUTE = null;
52 private static final String ANCESTOR_CM_HANDLES = "/ancestor::cm-handles";
56 public Map<String, NcmpServiceCmHandle> queryCmHandlePublicProperties(
57 final Map<String, String> publicPropertyQueryPairs) {
58 if (publicPropertyQueryPairs.isEmpty()) {
59 return Collections.emptyMap();
61 Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandles = null;
62 for (final Map.Entry<String, String> publicPropertyQueryPair : publicPropertyQueryPairs.entrySet()) {
63 final String cpsPath = "//public-properties[@name=\"" + publicPropertyQueryPair.getKey()
64 + "\" and @value=\"" + publicPropertyQueryPair.getValue() + "\"]";
66 final Collection<DataNode> dataNodes = queryCmHandleDataNodesByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS);
67 if (cmHandleIdToNcmpServiceCmHandles == null) {
68 cmHandleIdToNcmpServiceCmHandles = collectDataNodesToNcmpServiceCmHandles(dataNodes);
70 final Collection<String> cmHandleIdsToRetain = dataNodes.parallelStream()
71 .map(dataNode -> dataNode.getLeaves().get("id").toString()).collect(Collectors.toSet());
72 cmHandleIdToNcmpServiceCmHandles.keySet().retainAll(cmHandleIdsToRetain);
74 if (cmHandleIdToNcmpServiceCmHandles.isEmpty()) {
78 return cmHandleIdToNcmpServiceCmHandles;
82 public Map<String, NcmpServiceCmHandle> combineCmHandleQueries(final Map<String, NcmpServiceCmHandle> firstQuery,
83 final Map<String, NcmpServiceCmHandle> secondQuery) {
84 if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) {
85 return NO_QUERY_TO_EXECUTE;
86 } else if (firstQuery == NO_QUERY_TO_EXECUTE) {
88 } else if (secondQuery == NO_QUERY_TO_EXECUTE) {
91 firstQuery.keySet().retainAll(secondQuery.keySet());
97 public List<DataNode> queryCmHandlesByState(final CmHandleState cmHandleState) {
98 return queryCmHandleDataNodesByCpsPath("//state[@cm-handle-state=\"" + cmHandleState + "\"]",
99 INCLUDE_ALL_DESCENDANTS);
103 public List<DataNode> queryCmHandleDataNodesByCpsPath(final String cpsPath,
104 final FetchDescendantsOption fetchDescendantsOption) {
105 return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
106 cpsPath + ANCESTOR_CM_HANDLES, fetchDescendantsOption);
110 public boolean cmHandleHasState(final String cmHandleId, final CmHandleState requiredCmHandleState) {
111 final DataNode stateDataNode = getCmHandleState(cmHandleId);
112 final String cmHandleStateAsString = (String) stateDataNode.getLeaves().get("cm-handle-state");
113 return CmHandleState.valueOf(cmHandleStateAsString).equals(requiredCmHandleState);
117 public List<DataNode> queryCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) {
118 return queryCmHandleDataNodesByCpsPath("//state/datastores" + "/operational[@sync-state=\""
119 + dataStoreSyncState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS);
122 private Map<String, NcmpServiceCmHandle> collectDataNodesToNcmpServiceCmHandles(
123 final Collection<DataNode> dataNodes) {
124 final Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandle = new HashMap<>();
125 dataNodes.forEach(dataNode -> {
126 final NcmpServiceCmHandle ncmpServiceCmHandle = createNcmpServiceCmHandle(dataNode);
127 cmHandleIdToNcmpServiceCmHandle.put(ncmpServiceCmHandle.getCmHandleId(), ncmpServiceCmHandle);
129 return cmHandleIdToNcmpServiceCmHandle;
132 private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) {
133 return convertYangModelCmHandleToNcmpServiceCmHandle(YangDataConverter
134 .convertCmHandleToYangModel(dataNode, dataNode.getLeaves().get("id").toString()));
138 public Set<NcmpServiceCmHandle> getCmHandlesByDmiPluginIdentifier(final String dmiPluginIdentifier) {
139 final Map<String, DataNode> cmHandleAsDataNodePerCmHandleId = new HashMap<>();
140 for (final ModelledDmiServiceLeaves modelledDmiServiceLeaf : ModelledDmiServiceLeaves.values()) {
141 for (final DataNode cmHandleAsDataNode: getCmHandlesByDmiPluginIdentifierAndDmiProperty(
143 modelledDmiServiceLeaf.getLeafName())) {
144 cmHandleAsDataNodePerCmHandleId.put(
145 cmHandleAsDataNode.getLeaves().get("id").toString(), cmHandleAsDataNode);
148 final Set<NcmpServiceCmHandle> ncmpServiceCmHandles = new HashSet<>(cmHandleAsDataNodePerCmHandleId.size());
149 cmHandleAsDataNodePerCmHandleId.values().forEach(
150 cmHandleAsDataNode -> ncmpServiceCmHandles.add(createNcmpServiceCmHandle(cmHandleAsDataNode)));
151 return ncmpServiceCmHandles;
154 private List<DataNode> getCmHandlesByDmiPluginIdentifierAndDmiProperty(final String dmiPluginIdentifier,
155 final String dmiProperty) {
156 return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
157 "/dmi-registry/cm-handles[@" + dmiProperty + "='" + dmiPluginIdentifier + "']",
161 private DataNode getCmHandleState(final String cmHandleId) {
162 final String xpath = "/dmi-registry/cm-handles[@id='" + cmHandleId + "']/state";
163 return cpsDataPersistenceService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
164 xpath, OMIT_DESCENDANTS);