CM Data Subscriptions PoC/Performance test fixes
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / CmHandleQueriesImpl.java
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.inventory;
23
24 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
25 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS;
26
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Map;
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;
39
40 @RequiredArgsConstructor
41 @Component
42 public class CmHandleQueriesImpl implements CmHandleQueries {
43
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 = "//";
47
48     private final CpsDataPersistenceService cpsDataPersistenceService;
49     private static final String ANCESTOR_CM_HANDLES = "/ancestor::cm-handles";
50
51     @Override
52     public Collection<String> queryCmHandleAdditionalProperties(final Map<String, String> privatePropertyQueryPairs) {
53         return queryCmHandleAnyProperties(privatePropertyQueryPairs, PropertyType.ADDITIONAL);
54     }
55
56     @Override
57     public Collection<String> queryCmHandlePublicProperties(final Map<String, String> publicPropertyQueryPairs) {
58         return queryCmHandleAnyProperties(publicPropertyQueryPairs, PropertyType.PUBLIC);
59     }
60
61     @Override
62     public List<DataNode> queryCmHandlesByState(final CmHandleState cmHandleState) {
63         return queryCmHandleDataNodesByCpsPath("//state[@cm-handle-state=\"" + cmHandleState + "\"]",
64             INCLUDE_ALL_DESCENDANTS);
65     }
66
67     @Override
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);
72     }
73
74     @Override
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);
79     }
80
81     @Override
82     public List<DataNode> queryCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) {
83         return queryCmHandleDataNodesByCpsPath("//state/datastores" + "/operational[@sync-state=\""
84                 + dataStoreSyncState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS);
85     }
86
87     @Override
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(
92                     dmiPluginIdentifier,
93                     modelledDmiServiceLeaf.getLeafName())) {
94                 cmHandleIds.add(cmHandleAsDataNode.getLeaves().get("id").toString());
95             }
96         }
97         return cmHandleIds;
98     }
99
100     private Collection<String> collectCmHandleIdsFromDataNodes(final Collection<DataNode> dataNodes) {
101         return dataNodes.stream().map(dataNode -> (String) dataNode.getLeaves().get("id")).collect(Collectors.toSet());
102     }
103
104     private Collection<String> queryCmHandleAnyProperties(
105         final Map<String, String> propertyQueryPairs,
106         final PropertyType propertyType) {
107         if (propertyQueryPairs.isEmpty()) {
108             return Collections.emptySet();
109         }
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() + "\"]";
115
116             final Collection<DataNode> dataNodes = queryCmHandleDataNodesByCpsPath(cpsPath, OMIT_DESCENDANTS);
117             if (cmHandleIds == null) {
118                 cmHandleIds = collectCmHandleIdsFromDataNodes(dataNodes);
119             } else {
120                 final Collection<String> cmHandleIdsToRetain = collectCmHandleIdsFromDataNodes(dataNodes);
121                 cmHandleIds.retainAll(cmHandleIdsToRetain);
122             }
123             if (cmHandleIds.isEmpty()) {
124                 break;
125             }
126         }
127         return cmHandleIds;
128     }
129
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 + "']",
134                 OMIT_DESCENDANTS);
135     }
136
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();
141     }
142 }
143
144