245161747dadd7748ab9585034ed3621d03e1be4
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / CmHandleQueries.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.inventory;
22
23 import static org.onap.cps.ncmp.api.impl.utils.YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle;
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.HashMap;
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.impl.utils.YangDataConverter;
35 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
36 import org.onap.cps.spi.CpsDataPersistenceService;
37 import org.onap.cps.spi.FetchDescendantsOption;
38 import org.onap.cps.spi.model.DataNode;
39 import org.springframework.stereotype.Component;
40
41 @RequiredArgsConstructor
42 @Component
43 public class CmHandleQueries {
44
45     private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
46     private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
47
48     private final CpsDataPersistenceService cpsDataPersistenceService;
49     private static final Map<String, NcmpServiceCmHandle> NO_QUERY_TO_EXECUTE = null;
50     private static final String ANCESTOR_CM_HANDLES = "/ancestor::cm-handles";
51
52
53     /**
54      * Query CmHandles based on PublicProperties.
55      *
56      * @param publicPropertyQueryPairs public properties for query
57      * @return CmHandles which have these public properties
58      */
59     public Map<String, NcmpServiceCmHandle> queryCmHandlePublicProperties(
60         final Map<String, String> publicPropertyQueryPairs) {
61         if (publicPropertyQueryPairs.isEmpty()) {
62             return Collections.emptyMap();
63         }
64         Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandles = null;
65         for (final Map.Entry<String, String> publicPropertyQueryPair : publicPropertyQueryPairs.entrySet()) {
66             final String cpsPath = "//public-properties[@name=\"" + publicPropertyQueryPair.getKey()
67                 + "\" and @value=\"" + publicPropertyQueryPair.getValue() + "\"]";
68
69             final Collection<DataNode> dataNodes = queryCmHandleDataNodesByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS);
70             if (cmHandleIdToNcmpServiceCmHandles == null) {
71                 cmHandleIdToNcmpServiceCmHandles = collectDataNodesToNcmpServiceCmHandles(dataNodes);
72             } else {
73                 final Collection<String> cmHandleIdsToRetain = dataNodes.parallelStream()
74                     .map(dataNode -> dataNode.getLeaves().get("id").toString()).collect(Collectors.toSet());
75                 cmHandleIdToNcmpServiceCmHandles.keySet().retainAll(cmHandleIdsToRetain);
76             }
77             if (cmHandleIdToNcmpServiceCmHandles.isEmpty()) {
78                 break;
79             }
80         }
81         return cmHandleIdToNcmpServiceCmHandles;
82     }
83
84     /**
85      * Combine Maps of CmHandles.
86      *
87      * @param firstQuery first CmHandles Map
88      * @param secondQuery second CmHandles Map
89      * @return combined Map of CmHandles
90      */
91     public Map<String, NcmpServiceCmHandle> combineCmHandleQueries(
92         final Map<String, NcmpServiceCmHandle> firstQuery,
93         final Map<String, NcmpServiceCmHandle> secondQuery) {
94         if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) {
95             return Collections.emptyMap();
96         } else if (firstQuery == NO_QUERY_TO_EXECUTE) {
97             return secondQuery;
98         } else if (secondQuery == NO_QUERY_TO_EXECUTE) {
99             return firstQuery;
100         } else {
101             firstQuery.keySet().retainAll(secondQuery.keySet());
102             return firstQuery;
103         }
104     }
105
106     /**
107      * Method which returns cm handles by the cm handles state.
108      *
109      * @param cmHandleState cm handle state
110      * @return a list of cm handles
111      */
112     public List<DataNode> queryCmHandlesByState(final CmHandleState cmHandleState) {
113         return queryCmHandleDataNodesByCpsPath("//state[@cm-handle-state=\"" + cmHandleState + "\"]",
114             INCLUDE_ALL_DESCENDANTS);
115     }
116
117     /**
118      * Method to return data nodes representing the cm handles.
119      *
120      * @param cpsPath cps path for which the cmHandle is requested
121      * @return a list of data nodes representing the cm handles.
122      */
123     public List<DataNode> queryCmHandleDataNodesByCpsPath(final String cpsPath,
124                                                           final FetchDescendantsOption fetchDescendantsOption) {
125         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
126             cpsPath + ANCESTOR_CM_HANDLES, fetchDescendantsOption);
127     }
128
129     /**
130      * Method to check the state of a cm handle with given id.
131      *
132      * @param cmHandleId cm handle id
133      * @param requiredCmHandleState the required state of the cm handle
134      * @return a boolean, true if the state is equal to the required state
135      */
136     public boolean cmHandleHasState(final String cmHandleId, final CmHandleState requiredCmHandleState) {
137         final DataNode stateDataNode = getCmHandleState(cmHandleId);
138         final String cmHandleStateAsString = (String) stateDataNode.getLeaves().get("cm-handle-state");
139         return CmHandleState.valueOf(cmHandleStateAsString).equals(requiredCmHandleState);
140     }
141
142     /**
143      * Method which returns cm handles by the operational sync state of cm handle.
144      * @param dataStoreSyncState sync state
145      * @return a list of cm handles
146      */
147     public List<DataNode> queryCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) {
148         return queryCmHandleDataNodesByCpsPath("//state/datastores" + "/operational[@sync-state=\""
149                 + dataStoreSyncState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS);
150     }
151
152     private Map<String, NcmpServiceCmHandle> collectDataNodesToNcmpServiceCmHandles(
153         final Collection<DataNode> dataNodes) {
154         final Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandle = new HashMap<>();
155         dataNodes.forEach(dataNode -> {
156             final NcmpServiceCmHandle ncmpServiceCmHandle = createNcmpServiceCmHandle(dataNode);
157             cmHandleIdToNcmpServiceCmHandle.put(ncmpServiceCmHandle.getCmHandleId(), ncmpServiceCmHandle);
158         });
159         return cmHandleIdToNcmpServiceCmHandle;
160     }
161
162     private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) {
163         return convertYangModelCmHandleToNcmpServiceCmHandle(YangDataConverter
164             .convertCmHandleToYangModel(dataNode, dataNode.getLeaves().get("id").toString()));
165     }
166
167     private DataNode getCmHandleState(final String cmHandleId) {
168         final String xpath = "/dmi-registry/cm-handles[@id='" + cmHandleId + "']/state";
169         return cpsDataPersistenceService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
170                 xpath, OMIT_DESCENDANTS);
171     }
172 }
173
174