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