Merge "RTD change to document migration to Spring Boot 3.0"
[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.spi.CpsDataPersistenceService;
39 import org.onap.cps.spi.FetchDescendantsOption;
40 import org.onap.cps.spi.model.DataNode;
41 import org.springframework.stereotype.Component;
42
43 @RequiredArgsConstructor
44 @Component
45 public class CmHandleQueriesImpl implements CmHandleQueries {
46
47     private static final String DESCENDANT_PATH = "//";
48
49     private final CpsDataPersistenceService cpsDataPersistenceService;
50     private static final String ANCESTOR_CM_HANDLES = "/ancestor::cm-handles";
51
52     @Override
53     public Collection<String> queryCmHandleAdditionalProperties(final Map<String, String> privatePropertyQueryPairs) {
54         return queryCmHandleAnyProperties(privatePropertyQueryPairs, PropertyType.ADDITIONAL);
55     }
56
57     @Override
58     public Collection<String> queryCmHandlePublicProperties(final Map<String, String> publicPropertyQueryPairs) {
59         return queryCmHandleAnyProperties(publicPropertyQueryPairs, PropertyType.PUBLIC);
60     }
61
62     @Override
63     public List<DataNode> queryCmHandlesByState(final CmHandleState cmHandleState) {
64         return queryCmHandleDataNodesByCpsPath("//state[@cm-handle-state=\"" + cmHandleState + "\"]",
65             INCLUDE_ALL_DESCENDANTS);
66     }
67
68     @Override
69     public List<DataNode> queryCmHandleDataNodesByCpsPath(final String cpsPath,
70             final FetchDescendantsOption fetchDescendantsOption) {
71         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
72             cpsPath + ANCESTOR_CM_HANDLES, fetchDescendantsOption);
73     }
74
75     @Override
76     public boolean cmHandleHasState(final String cmHandleId, final CmHandleState requiredCmHandleState) {
77         final DataNode stateDataNode = getCmHandleState(cmHandleId);
78         final String cmHandleStateAsString = (String) stateDataNode.getLeaves().get("cm-handle-state");
79         return CmHandleState.valueOf(cmHandleStateAsString).equals(requiredCmHandleState);
80     }
81
82     @Override
83     public List<DataNode> queryCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) {
84         return queryCmHandleDataNodesByCpsPath("//state/datastores" + "/operational[@sync-state=\""
85                 + dataStoreSyncState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS);
86     }
87
88     @Override
89     public Collection<String> getCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
90         final Collection<String> cmHandleIds = new HashSet<>();
91         for (final ModelledDmiServiceLeaves modelledDmiServiceLeaf : ModelledDmiServiceLeaves.values()) {
92             for (final DataNode cmHandleAsDataNode: getCmHandlesByDmiPluginIdentifierAndDmiProperty(
93                     dmiPluginIdentifier,
94                     modelledDmiServiceLeaf.getLeafName())) {
95                 cmHandleIds.add(cmHandleAsDataNode.getLeaves().get("id").toString());
96             }
97         }
98         return cmHandleIds;
99     }
100
101     private Collection<String> collectCmHandleIdsFromDataNodes(final Collection<DataNode> dataNodes) {
102         return dataNodes.stream().map(dataNode -> (String) dataNode.getLeaves().get("id")).collect(Collectors.toSet());
103     }
104
105     private Collection<String> queryCmHandleAnyProperties(
106         final Map<String, String> propertyQueryPairs,
107         final PropertyType propertyType) {
108         if (propertyQueryPairs.isEmpty()) {
109             return Collections.emptySet();
110         }
111         Collection<String> cmHandleIds = null;
112         for (final Map.Entry<String, String> publicPropertyQueryPair : propertyQueryPairs.entrySet()) {
113             final String cpsPath = DESCENDANT_PATH + propertyType.getYangContainerName() + "[@name=\""
114                 + publicPropertyQueryPair.getKey()
115                 + "\" and @value=\"" + publicPropertyQueryPair.getValue() + "\"]";
116
117             final Collection<DataNode> dataNodes = queryCmHandleDataNodesByCpsPath(cpsPath, OMIT_DESCENDANTS);
118             if (cmHandleIds == null) {
119                 cmHandleIds = collectCmHandleIdsFromDataNodes(dataNodes);
120             } else {
121                 final Collection<String> cmHandleIdsToRetain = collectCmHandleIdsFromDataNodes(dataNodes);
122                 cmHandleIds.retainAll(cmHandleIdsToRetain);
123             }
124             if (cmHandleIds.isEmpty()) {
125                 break;
126             }
127         }
128         return cmHandleIds;
129     }
130
131     private List<DataNode> getCmHandlesByDmiPluginIdentifierAndDmiProperty(final String dmiPluginIdentifier,
132                                                                            final String dmiProperty) {
133         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
134                 NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@" + dmiProperty + "='" + dmiPluginIdentifier + "']",
135                 OMIT_DESCENDANTS);
136     }
137
138     private DataNode getCmHandleState(final String cmHandleId) {
139         final String xpath = NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']/state";
140         return cpsDataPersistenceService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
141                 xpath, OMIT_DESCENDANTS).iterator().next();
142     }
143 }
144
145