Merge "Add withTrustLevel condition to CmHandle Query API"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / 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.impl.inventory;
23
24 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DATASPACE_NAME;
25 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_ANCHOR;
26 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT;
27 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
28 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS;
29
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.stream.Collectors;
36 import lombok.RequiredArgsConstructor;
37 import org.onap.cps.ncmp.api.impl.inventory.enums.PropertyType;
38 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel;
39 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevelFilter;
40 import org.onap.cps.spi.CpsDataPersistenceService;
41 import org.onap.cps.spi.FetchDescendantsOption;
42 import org.onap.cps.spi.model.DataNode;
43 import org.springframework.stereotype.Component;
44
45 @RequiredArgsConstructor
46 @Component
47 public class CmHandleQueriesImpl implements CmHandleQueries {
48
49     private static final String DESCENDANT_PATH = "//";
50     private static final String ANCESTOR_CM_HANDLES = "/ancestor::cm-handles";
51     private final CpsDataPersistenceService cpsDataPersistenceService;
52     private final Map<String, TrustLevel> trustLevelPerCmHandle;
53
54     @Override
55     public Collection<String> queryCmHandleAdditionalProperties(final Map<String, String> privatePropertyQueryPairs) {
56         return queryCmHandleAnyProperties(privatePropertyQueryPairs, PropertyType.ADDITIONAL);
57     }
58
59     @Override
60     public Collection<String> queryCmHandlePublicProperties(final Map<String, String> publicPropertyQueryPairs) {
61         return queryCmHandleAnyProperties(publicPropertyQueryPairs, PropertyType.PUBLIC);
62     }
63
64     @Override
65     public Collection<String> queryCmHandlesByTrustLevel(final Map<String, String> trustLevelPropertyQueryPairs) {
66         final String trustLevelProperty = trustLevelPropertyQueryPairs.values().iterator().next();
67         final TrustLevel targetTrustLevel = TrustLevel.valueOf(trustLevelProperty);
68
69         final TrustLevelFilter trustLevelFilter = new TrustLevelFilter(targetTrustLevel, trustLevelPerCmHandle);
70         return trustLevelFilter.getAllCmHandleIdsByTargetTrustLevel();
71     }
72
73     @Override
74     public List<DataNode> queryCmHandlesByState(final CmHandleState cmHandleState) {
75         return queryCmHandleAncestorsByCpsPath("//state[@cm-handle-state=\"" + cmHandleState + "\"]",
76             INCLUDE_ALL_DESCENDANTS);
77     }
78
79     @Override
80     public List<DataNode> queryNcmpRegistryByCpsPath(final String cpsPath,
81                                                      final FetchDescendantsOption fetchDescendantsOption) {
82         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
83                 cpsPath, fetchDescendantsOption);
84     }
85
86     @Override
87     public List<DataNode> queryCmHandleAncestorsByCpsPath(final String cpsPath,
88                                                           final FetchDescendantsOption fetchDescendantsOption) {
89         return queryNcmpRegistryByCpsPath(cpsPath + ANCESTOR_CM_HANDLES, fetchDescendantsOption);
90     }
91
92     @Override
93     public boolean cmHandleHasState(final String cmHandleId, final CmHandleState requiredCmHandleState) {
94         final DataNode stateDataNode = getCmHandleState(cmHandleId);
95         final String cmHandleStateAsString = (String) stateDataNode.getLeaves().get("cm-handle-state");
96         return CmHandleState.valueOf(cmHandleStateAsString).equals(requiredCmHandleState);
97     }
98
99     @Override
100     public List<DataNode> queryCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) {
101         return queryCmHandleAncestorsByCpsPath("//state/datastores" + "/operational[@sync-state=\""
102                 + dataStoreSyncState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS);
103     }
104
105     @Override
106     public Collection<String> getCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
107         final Collection<String> cmHandleIds = new HashSet<>();
108         for (final ModelledDmiServiceLeaves modelledDmiServiceLeaf : ModelledDmiServiceLeaves.values()) {
109             for (final DataNode cmHandleAsDataNode: getCmHandlesByDmiPluginIdentifierAndDmiProperty(
110                     dmiPluginIdentifier,
111                     modelledDmiServiceLeaf.getLeafName())) {
112                 cmHandleIds.add(cmHandleAsDataNode.getLeaves().get("id").toString());
113             }
114         }
115         return cmHandleIds;
116     }
117
118     private Collection<String> collectCmHandleIdsFromDataNodes(final Collection<DataNode> dataNodes) {
119         return dataNodes.stream().map(dataNode -> (String) dataNode.getLeaves().get("id")).collect(Collectors.toSet());
120     }
121
122     private Collection<String> queryCmHandleAnyProperties(
123         final Map<String, String> propertyQueryPairs,
124         final PropertyType propertyType) {
125         if (propertyQueryPairs.isEmpty()) {
126             return Collections.emptySet();
127         }
128         Collection<String> cmHandleIds = null;
129         for (final Map.Entry<String, String> publicPropertyQueryPair : propertyQueryPairs.entrySet()) {
130             final String cpsPath = DESCENDANT_PATH + propertyType.getYangContainerName() + "[@name=\""
131                 + publicPropertyQueryPair.getKey()
132                 + "\" and @value=\"" + publicPropertyQueryPair.getValue() + "\"]";
133
134             final Collection<DataNode> dataNodes = queryCmHandleAncestorsByCpsPath(cpsPath,
135                     OMIT_DESCENDANTS);
136             if (cmHandleIds == null) {
137                 cmHandleIds = collectCmHandleIdsFromDataNodes(dataNodes);
138             } else {
139                 final Collection<String> cmHandleIdsToRetain = collectCmHandleIdsFromDataNodes(dataNodes);
140                 cmHandleIds.retainAll(cmHandleIdsToRetain);
141             }
142             if (cmHandleIds.isEmpty()) {
143                 break;
144             }
145         }
146         return cmHandleIds;
147     }
148
149     private List<DataNode> getCmHandlesByDmiPluginIdentifierAndDmiProperty(final String dmiPluginIdentifier,
150                                                                            final String dmiProperty) {
151         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
152                 NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@" + dmiProperty + "='" + dmiPluginIdentifier + "']",
153                 OMIT_DESCENDANTS);
154     }
155
156     private DataNode getCmHandleState(final String cmHandleId) {
157         final String xpath = NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']/state";
158         return cpsDataPersistenceService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
159                 xpath, OMIT_DESCENDANTS).iterator().next();
160     }
161 }
162
163