92387bab323c2459571f7039cb2f2997cd37bea2
[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
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.stream.Collectors;
32 import lombok.RequiredArgsConstructor;
33 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
34 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
35 import org.onap.cps.spi.CpsDataPersistenceService;
36 import org.onap.cps.spi.FetchDescendantsOption;
37 import org.onap.cps.spi.model.DataNode;
38 import org.springframework.stereotype.Component;
39
40 @RequiredArgsConstructor
41 @Component
42 public class CmHandleQueries {
43
44     private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
45     private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
46
47     private final CpsDataPersistenceService cpsDataPersistenceService;
48     private static final Map<String, NcmpServiceCmHandle> NO_QUERY_TO_EXECUTE = null;
49     private static final String ANCESTOR_CM_HANDLES = "/ancestor::cm-handles";
50
51
52     /**
53      * Query CmHandles based on PublicProperties.
54      *
55      * @param publicPropertyQueryPairs public properties for query
56      * @return CmHandles which have these public properties
57      */
58     public Map<String, NcmpServiceCmHandle> queryCmHandlePublicProperties(
59         final Map<String, String> publicPropertyQueryPairs) {
60         if (publicPropertyQueryPairs.isEmpty()) {
61             return Collections.emptyMap();
62         }
63         Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandles = null;
64         for (final Map.Entry<String, String> publicPropertyQueryPair : publicPropertyQueryPairs.entrySet()) {
65             final String cpsPath = "//public-properties[@name=\"" + publicPropertyQueryPair.getKey()
66                 + "\" and @value=\"" + publicPropertyQueryPair.getValue() + "\"]";
67
68             final Collection<DataNode> dataNodes = getCmHandleDataNodesByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS);
69             if (cmHandleIdToNcmpServiceCmHandles == null) {
70                 cmHandleIdToNcmpServiceCmHandles = collectDataNodesToNcmpServiceCmHandles(dataNodes);
71             } else {
72                 final Collection<String> cmHandleIdsToRetain = dataNodes.parallelStream()
73                     .map(dataNode -> dataNode.getLeaves().get("id").toString()).collect(Collectors.toSet());
74                 cmHandleIdToNcmpServiceCmHandles.keySet().retainAll(cmHandleIdsToRetain);
75             }
76             if (cmHandleIdToNcmpServiceCmHandles.isEmpty()) {
77                 break;
78             }
79         }
80         return cmHandleIdToNcmpServiceCmHandles;
81     }
82
83     /**
84      * Combine Maps of CmHandles.
85      *
86      * @param firstQuery first CmHandles Map
87      * @param secondQuery second CmHandles Map
88      * @return combined Map of CmHandles
89      */
90     public Map<String, NcmpServiceCmHandle> combineCmHandleQueries(
91         final Map<String, NcmpServiceCmHandle> firstQuery,
92         final Map<String, NcmpServiceCmHandle> secondQuery) {
93         if (firstQuery == NO_QUERY_TO_EXECUTE && secondQuery == NO_QUERY_TO_EXECUTE) {
94             return Collections.emptyMap();
95         } else if (firstQuery == NO_QUERY_TO_EXECUTE) {
96             return secondQuery;
97         } else if (secondQuery == NO_QUERY_TO_EXECUTE) {
98             return firstQuery;
99         } else {
100             firstQuery.keySet().retainAll(secondQuery.keySet());
101             return firstQuery;
102         }
103     }
104
105     /**
106      * Method which returns cm handles by the cm handles state.
107      *
108      * @param cmHandleState cm handle state
109      * @return a list of cm handles
110      */
111     public List<DataNode> getCmHandlesByState(final CmHandleState cmHandleState) {
112         return getCmHandleDataNodesByCpsPath("//state[@cm-handle-state=\"" + cmHandleState + "\"]",
113             INCLUDE_ALL_DESCENDANTS);
114     }
115
116     /**
117      * Method to return data nodes representing the cm handles.
118      *
119      * @param cpsPath cps path for which the cmHandle is requested
120      * @return a list of data nodes representing the cm handles.
121      */
122     public List<DataNode> getCmHandleDataNodesByCpsPath(final String cpsPath,
123                                                         final FetchDescendantsOption fetchDescendantsOption) {
124         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
125             cpsPath + ANCESTOR_CM_HANDLES, fetchDescendantsOption);
126     }
127
128     /**
129      * Method which returns cm handles by the cm handle id and state.
130      * @param cmHandleId cm handle id
131      * @param cmHandleState cm handle state
132      * @return a list of cm handles
133      */
134     public List<DataNode> getCmHandlesByIdAndState(final String cmHandleId, final CmHandleState cmHandleState) {
135         return getCmHandleDataNodesByCpsPath("//cm-handles[@id='" + cmHandleId + "']/state[@cm-handle-state=\""
136                 + cmHandleState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS);
137     }
138
139     /**
140      * Method which returns cm handles by the operational sync state of cm handle.
141      * @param dataStoreSyncState sync state
142      * @return a list of cm handles
143      */
144     public List<DataNode> getCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) {
145         return getCmHandleDataNodesByCpsPath("//state/datastores" + "/operational[@sync-state=\""
146                 + dataStoreSyncState + "\"]", FetchDescendantsOption.OMIT_DESCENDANTS);
147     }
148
149     private Map<String, NcmpServiceCmHandle> collectDataNodesToNcmpServiceCmHandles(
150         final Collection<DataNode> dataNodes) {
151         final Map<String, NcmpServiceCmHandle> cmHandleIdToNcmpServiceCmHandle = new HashMap<>();
152         dataNodes.forEach(dataNode -> {
153             final NcmpServiceCmHandle ncmpServiceCmHandle = createNcmpServiceCmHandle(dataNode);
154             cmHandleIdToNcmpServiceCmHandle.put(ncmpServiceCmHandle.getCmHandleId(), ncmpServiceCmHandle);
155         });
156         return cmHandleIdToNcmpServiceCmHandle;
157     }
158
159     private NcmpServiceCmHandle createNcmpServiceCmHandle(final DataNode dataNode) {
160         return convertYangModelCmHandleToNcmpServiceCmHandle(YangDataConverter
161             .convertCmHandleToYangModel(dataNode, dataNode.getLeaves().get("id").toString()));
162     }
163 }
164
165