Uplift Spring boot 3.2.4
[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-2024 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.config.embeddedcache.TrustLevelCacheConfig;
38 import org.onap.cps.ncmp.api.impl.inventory.enums.PropertyType;
39 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel;
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.onap.cps.spi.utils.CpsValidator;
44 import org.springframework.beans.factory.annotation.Qualifier;
45 import org.springframework.stereotype.Component;
46
47 @RequiredArgsConstructor
48 @Component
49 public class CmHandleQueriesImpl implements CmHandleQueries {
50
51     private static final String DESCENDANT_PATH = "//";
52     private static final String ANCESTOR_CM_HANDLES = "/ancestor::cm-handles";
53     private final CpsDataPersistenceService cpsDataPersistenceService;
54
55     @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_DMI_PLUGIN)
56     private final Map<String, TrustLevel> trustLevelPerDmiPlugin;
57
58     @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_CM_HANDLE)
59     private final Map<String, TrustLevel> trustLevelPerCmHandle;
60
61     private final CpsValidator cpsValidator;
62
63     @Override
64     public Collection<String> queryCmHandleAdditionalProperties(final Map<String, String> privatePropertyQueryPairs) {
65         return queryCmHandleAnyProperties(privatePropertyQueryPairs, PropertyType.ADDITIONAL);
66     }
67
68     @Override
69     public Collection<String> queryCmHandlePublicProperties(final Map<String, String> publicPropertyQueryPairs) {
70         return queryCmHandleAnyProperties(publicPropertyQueryPairs, PropertyType.PUBLIC);
71     }
72
73     @Override
74     public Collection<String> queryCmHandlesByTrustLevel(final Map<String, String> trustLevelPropertyQueryPairs) {
75         final String trustLevelProperty = trustLevelPropertyQueryPairs.values().iterator().next();
76         final TrustLevel targetTrustLevel = TrustLevel.valueOf(trustLevelProperty);
77
78         return getCmHandleIdsByTrustLevel(targetTrustLevel);
79     }
80
81     @Override
82     public List<DataNode> queryCmHandlesByState(final CmHandleState cmHandleState) {
83         return queryCmHandleAncestorsByCpsPath("//state[@cm-handle-state=\"" + cmHandleState + "\"]",
84             INCLUDE_ALL_DESCENDANTS);
85     }
86
87     @Override
88     public List<DataNode> queryNcmpRegistryByCpsPath(final String cpsPath,
89                                                      final FetchDescendantsOption fetchDescendantsOption) {
90         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
91                 cpsPath, fetchDescendantsOption);
92     }
93
94     @Override
95     public List<DataNode> queryCmHandleAncestorsByCpsPath(final String cpsPath,
96                                                           final FetchDescendantsOption fetchDescendantsOption) {
97         return queryNcmpRegistryByCpsPath(cpsPath + ANCESTOR_CM_HANDLES, fetchDescendantsOption);
98     }
99
100     @Override
101     public boolean cmHandleHasState(final String cmHandleId, final CmHandleState requiredCmHandleState) {
102         final DataNode stateDataNode = getCmHandleState(cmHandleId);
103         final String cmHandleStateAsString = (String) stateDataNode.getLeaves().get("cm-handle-state");
104         return CmHandleState.valueOf(cmHandleStateAsString).equals(requiredCmHandleState);
105     }
106
107     @Override
108     public List<DataNode> queryCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) {
109         return queryCmHandleAncestorsByCpsPath("//state/datastores" + "/operational[@sync-state=\""
110                 + dataStoreSyncState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS);
111     }
112
113     @Override
114     public Collection<String> getCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
115         final Collection<String> cmHandleIds = new HashSet<>();
116         for (final ModelledDmiServiceLeaves modelledDmiServiceLeaf : ModelledDmiServiceLeaves.values()) {
117             for (final DataNode cmHandleAsDataNode: getCmHandlesByDmiPluginIdentifierAndDmiProperty(
118                     dmiPluginIdentifier,
119                     modelledDmiServiceLeaf.getLeafName())) {
120                 cmHandleIds.add(cmHandleAsDataNode.getLeaves().get("id").toString());
121             }
122         }
123         return cmHandleIds;
124     }
125
126     private Collection<String> getCmHandleIdsByTrustLevel(final TrustLevel targetTrustLevel) {
127         final Collection<String> selectedCmHandleIds = new HashSet<>();
128
129         for (final Map.Entry<String, TrustLevel> mapEntry : trustLevelPerDmiPlugin.entrySet()) {
130             final String dmiPluginIdentifier = mapEntry.getKey();
131             final TrustLevel dmiTrustLevel = mapEntry.getValue();
132             final Collection<String> candidateCmHandleIds = getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifier);
133             for (final String candidateCmHandleId : candidateCmHandleIds) {
134                 final TrustLevel candidateCmHandleTrustLevel = trustLevelPerCmHandle.get(candidateCmHandleId);
135                 final TrustLevel effectiveTrustlevel =
136                     candidateCmHandleTrustLevel.getEffectiveTrustLevel(dmiTrustLevel);
137                 if (targetTrustLevel.equals(effectiveTrustlevel)) {
138                     selectedCmHandleIds.add(candidateCmHandleId);
139                 }
140             }
141         }
142
143         return selectedCmHandleIds;
144     }
145
146     private Collection<String> collectCmHandleIdsFromDataNodes(final Collection<DataNode> dataNodes) {
147         return dataNodes.stream().map(dataNode -> (String) dataNode.getLeaves().get("id")).collect(Collectors.toSet());
148     }
149
150     private Collection<String> queryCmHandleAnyProperties(
151         final Map<String, String> propertyQueryPairs,
152         final PropertyType propertyType) {
153         if (propertyQueryPairs.isEmpty()) {
154             return Collections.emptySet();
155         }
156         Collection<String> cmHandleIds = null;
157         for (final Map.Entry<String, String> publicPropertyQueryPair : propertyQueryPairs.entrySet()) {
158             final String cpsPath = DESCENDANT_PATH + propertyType.getYangContainerName() + "[@name=\""
159                 + publicPropertyQueryPair.getKey()
160                 + "\" and @value=\"" + publicPropertyQueryPair.getValue() + "\"]";
161
162             final Collection<DataNode> dataNodes = queryCmHandleAncestorsByCpsPath(cpsPath,
163                     OMIT_DESCENDANTS);
164             if (cmHandleIds == null) {
165                 cmHandleIds = collectCmHandleIdsFromDataNodes(dataNodes);
166             } else {
167                 final Collection<String> cmHandleIdsToRetain = collectCmHandleIdsFromDataNodes(dataNodes);
168                 cmHandleIds.retainAll(cmHandleIdsToRetain);
169             }
170             if (cmHandleIds.isEmpty()) {
171                 break;
172             }
173         }
174         return cmHandleIds;
175     }
176
177     private List<DataNode> getCmHandlesByDmiPluginIdentifierAndDmiProperty(final String dmiPluginIdentifier,
178                                                                            final String dmiProperty) {
179         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
180                 NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@" + dmiProperty + "='" + dmiPluginIdentifier + "']",
181                 OMIT_DESCENDANTS);
182     }
183
184     private DataNode getCmHandleState(final String cmHandleId) {
185         cpsValidator.validateNameCharacters(cmHandleId);
186         final String xpath = NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']/state";
187         return cpsDataPersistenceService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
188                 xpath, OMIT_DESCENDANTS).iterator().next();
189     }
190 }
191
192