2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2023 Nordix Foundation
4 * Modifications Copyright (C) 2023 TechMahindra Ltd.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.inventory;
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.HashSet;
30 import java.util.List;
32 import java.util.stream.Collectors;
33 import lombok.RequiredArgsConstructor;
34 import org.onap.cps.ncmp.api.inventory.enums.PropertyType;
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 CmHandleQueriesImpl implements CmHandleQueries {
44 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
45 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
46 private static final String DESCENDANT_PATH = "//";
48 private final CpsDataPersistenceService cpsDataPersistenceService;
49 private static final String ANCESTOR_CM_HANDLES = "/ancestor::cm-handles";
52 public Collection<String> queryCmHandleAdditionalProperties(final Map<String, String> privatePropertyQueryPairs) {
53 return queryCmHandleAnyProperties(privatePropertyQueryPairs, PropertyType.ADDITIONAL);
57 public Collection<String> queryCmHandlePublicProperties(final Map<String, String> publicPropertyQueryPairs) {
58 return queryCmHandleAnyProperties(publicPropertyQueryPairs, PropertyType.PUBLIC);
62 public List<DataNode> queryCmHandlesByState(final CmHandleState cmHandleState) {
63 return queryCmHandleDataNodesByCpsPath("//state[@cm-handle-state=\"" + cmHandleState + "\"]",
64 INCLUDE_ALL_DESCENDANTS);
68 public List<DataNode> queryCmHandleDataNodesByCpsPath(final String cpsPath,
69 final FetchDescendantsOption fetchDescendantsOption) {
70 return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
71 cpsPath + ANCESTOR_CM_HANDLES, fetchDescendantsOption);
75 public boolean cmHandleHasState(final String cmHandleId, final CmHandleState requiredCmHandleState) {
76 final DataNode stateDataNode = getCmHandleState(cmHandleId);
77 final String cmHandleStateAsString = (String) stateDataNode.getLeaves().get("cm-handle-state");
78 return CmHandleState.valueOf(cmHandleStateAsString).equals(requiredCmHandleState);
82 public List<DataNode> queryCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) {
83 return queryCmHandleDataNodesByCpsPath("//state/datastores" + "/operational[@sync-state=\""
84 + dataStoreSyncState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS);
88 public Collection<String> getCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
89 final Collection<String> cmHandleIds = new HashSet<>();
90 for (final ModelledDmiServiceLeaves modelledDmiServiceLeaf : ModelledDmiServiceLeaves.values()) {
91 for (final DataNode cmHandleAsDataNode: getCmHandlesByDmiPluginIdentifierAndDmiProperty(
93 modelledDmiServiceLeaf.getLeafName())) {
94 cmHandleIds.add(cmHandleAsDataNode.getLeaves().get("id").toString());
100 private Collection<String> collectCmHandleIdsFromDataNodes(final Collection<DataNode> dataNodes) {
101 return dataNodes.stream().map(dataNode -> (String) dataNode.getLeaves().get("id")).collect(Collectors.toSet());
104 private Collection<String> queryCmHandleAnyProperties(
105 final Map<String, String> propertyQueryPairs,
106 final PropertyType propertyType) {
107 if (propertyQueryPairs.isEmpty()) {
108 return Collections.emptySet();
110 Collection<String> cmHandleIds = null;
111 for (final Map.Entry<String, String> publicPropertyQueryPair : propertyQueryPairs.entrySet()) {
112 final String cpsPath = DESCENDANT_PATH + propertyType.getYangContainerName() + "[@name=\""
113 + publicPropertyQueryPair.getKey()
114 + "\" and @value=\"" + publicPropertyQueryPair.getValue() + "\"]";
116 final Collection<DataNode> dataNodes = queryCmHandleDataNodesByCpsPath(cpsPath, OMIT_DESCENDANTS);
117 if (cmHandleIds == null) {
118 cmHandleIds = collectCmHandleIdsFromDataNodes(dataNodes);
120 final Collection<String> cmHandleIdsToRetain = collectCmHandleIdsFromDataNodes(dataNodes);
121 cmHandleIds.retainAll(cmHandleIdsToRetain);
123 if (cmHandleIds.isEmpty()) {
130 private List<DataNode> getCmHandlesByDmiPluginIdentifierAndDmiProperty(final String dmiPluginIdentifier,
131 final String dmiProperty) {
132 return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
133 "/dmi-registry/cm-handles[@" + dmiProperty + "='" + dmiPluginIdentifier + "']",
137 private DataNode getCmHandleState(final String cmHandleId) {
138 final String xpath = "/dmi-registry/cm-handles[@id='" + cmHandleId + "']/state";
139 return cpsDataPersistenceService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
140 xpath, OMIT_DESCENDANTS).iterator().next();