0487886ec01a5353b5558dccd77e889f89d5c914
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
4  *  Modifications Copyright (C) 2024 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.impl.datajobs.subscription.utils;
23
24 import static org.onap.cps.api.parameters.FetchDescendantsOption.OMIT_DESCENDANTS;
25 import static org.onap.cps.ncmp.impl.datajobs.subscription.models.CmSubscriptionStatus.UNKNOWN;
26
27 import java.io.Serializable;
28 import java.time.OffsetDateTime;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Map;
34 import lombok.RequiredArgsConstructor;
35 import lombok.extern.slf4j.Slf4j;
36 import org.onap.cps.api.CpsDataService;
37 import org.onap.cps.api.CpsQueryService;
38 import org.onap.cps.api.model.DataNode;
39 import org.onap.cps.ncmp.impl.datajobs.subscription.models.CmSubscriptionStatus;
40 import org.onap.cps.utils.ContentType;
41 import org.onap.cps.utils.JsonObjectMapper;
42 import org.springframework.stereotype.Service;
43
44 @Slf4j
45 @Service
46 @RequiredArgsConstructor
47 public class CmDataJobSubscriptionPersistenceService {
48
49     private static final String DATASPACE = "NCMP-Admin";
50     private static final String ANCHOR = "cm-data-job-subscriptions";
51
52     private static final String PARENT_NODE_XPATH = "/dataJob";
53     private static final String CPS_PATH_FOR_SUBSCRIPTION_NODE = "//subscription";
54     private static final String CPS_PATH_TEMPLATE_FOR_SUBSCRIPTIONS_WITH_DATA_NODE_SELECTOR =
55             CPS_PATH_FOR_SUBSCRIPTION_NODE + "[@dataNodeSelector='%s']";
56     private static final String CPS_PATH_FOR_SUBSCRIPTION_WITH_DATA_NODE_SELECTOR = PARENT_NODE_XPATH
57             + "/subscription[@dataNodeSelector='%s']";
58     private static final String CPS_PATH_TEMPLATE_FOR_SUBSCRIPTION_WITH_DATA_JOB_ID =
59             CPS_PATH_FOR_SUBSCRIPTION_NODE + "/dataJobId[text()='%s']";
60     private static final String CPS_PATH_TEMPLATE_FOR_INACTIVE_SUBSCRIPTIONS =
61             CPS_PATH_FOR_SUBSCRIPTION_NODE + "[@status='UNKNOWN' or @status='REJECTED']/dataJobId[text()='%s']";
62
63     private static final String DATANODE_SELECTOR_LEAF_NAME = "dataNodeSelector";
64     private static final String STATUS_LEAF_NAME = "status";
65     private static final String DATAJOB_ID_LEAF_NAME = "dataJobId";
66
67     private final JsonObjectMapper jsonObjectMapper;
68     private final CpsQueryService cpsQueryService;
69     private final CpsDataService cpsDataService;
70
71     /**
72      * Check if we have a cm data job subscription for the given data node selector.
73      *
74      * @param dataNodeSelector the target of the data job subscription
75      * @return true if the subscription details has at least one subscriber , otherwise false
76      */
77     public boolean hasAtLeastOneSubscription(final String dataNodeSelector) {
78         return !getSubscriptionIds(dataNodeSelector).isEmpty();
79     }
80
81     /**
82      * Check if the input is a new subscription ID against ongoing subscriptions.
83      *
84      * @param subscriptionId subscription ID
85      * @return true if subscriptionId is not used in active subscriptions, otherwise false
86      */
87     public boolean isNewSubscriptionId(final String subscriptionId) {
88         final String query = CPS_PATH_TEMPLATE_FOR_SUBSCRIPTION_WITH_DATA_JOB_ID.formatted(subscriptionId);
89         return cpsQueryService.queryDataNodes(DATASPACE, ANCHOR,
90                 query, OMIT_DESCENDANTS).isEmpty();
91     }
92
93     /**
94      * Get the ids for the subscriptions for the given data node selector.
95      *
96      * @param dataNodeSelector the target of the data job subscription
97      * @return collection of subscription ids of ongoing cm notification subscription
98      */
99     @SuppressWarnings("unchecked")
100     public Collection<String> getSubscriptionIds(final String dataNodeSelector) {
101         final String query = CPS_PATH_TEMPLATE_FOR_SUBSCRIPTIONS_WITH_DATA_NODE_SELECTOR.formatted(dataNodeSelector);
102         final Collection<DataNode> existingNodes =
103                 cpsQueryService.queryDataNodes(DATASPACE, ANCHOR,
104                         query, OMIT_DESCENDANTS);
105         if (existingNodes.isEmpty()) {
106             return Collections.emptyList();
107         }
108         return (Collection<String>) existingNodes.iterator().next().getLeaves().get(DATAJOB_ID_LEAF_NAME);
109     }
110
111     /**
112      * Get data node selectors for subscriptions with subscription ID.
113      *
114      * @param subscriptionId subscription ID
115      * @return a list of dataNodeSelectors, or empty list if none found
116      */
117     public Collection<String> getDataNodeSelectors(final String subscriptionId) {
118         final String query = CPS_PATH_TEMPLATE_FOR_SUBSCRIPTION_WITH_DATA_JOB_ID.formatted(subscriptionId);
119         final Collection<DataNode> dataNodes =
120                 cpsQueryService.queryDataNodes(DATASPACE, ANCHOR, query, OMIT_DESCENDANTS);
121         final List<String> dataNodeSelectors = new ArrayList<>();
122         for (final DataNode dataNode : dataNodes) {
123             final String dataNodeSelector = dataNode.getLeaves().get(DATANODE_SELECTOR_LEAF_NAME).toString();
124             dataNodeSelectors.add(dataNodeSelector);
125         }
126         return dataNodeSelectors;
127     }
128
129     /**
130      * Remove cm notification data job subscription.
131      *
132      * @param subscriptionId   data job subscription id to be deleted
133      * @param dataNodeSelector the target of the data job subscription
134      */
135     public void delete(final String subscriptionId, final String dataNodeSelector) {
136         final String query = CPS_PATH_TEMPLATE_FOR_SUBSCRIPTIONS_WITH_DATA_NODE_SELECTOR.formatted(dataNodeSelector);
137         final Collection<DataNode> dataNodes =
138                 cpsQueryService.queryDataNodes(DATASPACE, ANCHOR, query, OMIT_DESCENDANTS);
139         final Collection<String> subscriptionIds = getSubscriptionIds(dataNodeSelector);
140         if (!subscriptionIds.remove(subscriptionId)) {
141             log.warn("SubscriptionId={} not found under dataNodeSelector={}", subscriptionId, dataNodeSelector);
142             return;
143         }
144         if (subscriptionIds.isEmpty()) {
145             deleteEntireSubscription(dataNodeSelector);
146         } else {
147             final String currentStatus = dataNodes.iterator().next().getLeaves().get(STATUS_LEAF_NAME).toString();
148             updateSubscriptionDetails(dataNodeSelector, subscriptionIds, currentStatus);
149         }
150     }
151
152     /**
153      * Delete the entire subscription.
154      *
155      * @param dataNodeSelector data node selector
156      */
157     public void deleteEntireSubscription(final String dataNodeSelector) {
158         final String query = CPS_PATH_FOR_SUBSCRIPTION_WITH_DATA_NODE_SELECTOR.formatted(dataNodeSelector);
159         cpsDataService.deleteDataNode(DATASPACE, ANCHOR, query, OffsetDateTime.now());
160     }
161
162     /**
163      * Get data node selectors for subscriptions with status UNKNOWN or REJECTED.
164      *
165      * @param subscriptionId subscription ID
166      * @return a list of data node selectors
167      */
168     public List<String> getInactiveDataNodeSelectors(final String subscriptionId) {
169         final String query = CPS_PATH_TEMPLATE_FOR_INACTIVE_SUBSCRIPTIONS.formatted(subscriptionId);
170         final Collection<DataNode> dataNodes = cpsQueryService.queryDataNodes(DATASPACE, ANCHOR, query,
171                 OMIT_DESCENDANTS);
172         final List<String> dataNodeSelectors = new ArrayList<>(dataNodes.size());
173         for (final DataNode dataNode : dataNodes) {
174             final String dataNodeSelector = dataNode.getLeaves().get(DATANODE_SELECTOR_LEAF_NAME).toString();
175             dataNodeSelectors.add(dataNodeSelector);
176         }
177         return dataNodeSelectors;
178     }
179
180     /**
181      * Add cm notification data job subscription.
182      *
183      * @param subscriptionId   data job subscription id to be added
184      * @param dataNodeSelector the target of the data job subscription
185      */
186     public void add(final String subscriptionId, final String dataNodeSelector) {
187         final String query = CPS_PATH_TEMPLATE_FOR_SUBSCRIPTIONS_WITH_DATA_NODE_SELECTOR.formatted(dataNodeSelector);
188         final Collection<DataNode> dataNodes = cpsQueryService.queryDataNodes(DATASPACE, ANCHOR, query,
189                 OMIT_DESCENDANTS);
190         if (dataNodes.isEmpty()) {
191             addNewSubscriptionDetails(subscriptionId, dataNodeSelector);
192         } else {
193             final Collection<String> subscriptionIds = getSubscriptionIds(dataNodeSelector);
194             final String cmSubscriptionStatusName =
195                     dataNodes.iterator().next().getLeaves().get(STATUS_LEAF_NAME).toString();
196             subscriptionIds.add(subscriptionId);
197             updateSubscriptionDetails(dataNodeSelector, subscriptionIds, cmSubscriptionStatusName);
198         }
199     }
200
201     /**
202      * Update status of a subscription.
203      *
204      * @param dataNodeSelector     data node selector
205      * @param cmSubscriptionStatus cm subscription status
206      */
207     public void updateCmSubscriptionStatus(final String dataNodeSelector,
208                                            final CmSubscriptionStatus cmSubscriptionStatus) {
209         final Collection<String> subscriptionIds = getSubscriptionIds(dataNodeSelector);
210         updateSubscriptionDetails(dataNodeSelector, subscriptionIds, cmSubscriptionStatus.name());
211     }
212
213     private void addNewSubscriptionDetails(final String subscriptionId,
214                                            final String dataNodeSelector) {
215         final Collection<String> newSubscriptionList = Collections.singletonList(subscriptionId);
216         final String subscriptionDetailsAsJson = createSubscriptionDetailsAsJson(dataNodeSelector,
217                 newSubscriptionList, UNKNOWN.name());
218         cpsDataService.saveData(DATASPACE, ANCHOR, PARENT_NODE_XPATH, subscriptionDetailsAsJson,
219             OffsetDateTime.now(), ContentType.JSON);
220     }
221
222     private void updateSubscriptionDetails(final String dataNodeSelector, final Collection<String> subscriptionIds,
223                                            final String cmSubscriptionStatusName) {
224         final String subscriptionDetailsAsJson = createSubscriptionDetailsAsJson(dataNodeSelector,
225                 subscriptionIds, cmSubscriptionStatusName);
226         cpsDataService.updateNodeLeaves(DATASPACE, ANCHOR, PARENT_NODE_XPATH, subscriptionDetailsAsJson,
227                 OffsetDateTime.now(),
228             ContentType.JSON);
229     }
230
231     private String createSubscriptionDetailsAsJson(final String dataNodeSelector,
232                                                    final Collection<String> subscriptionIds,
233                                                    final String cmSubscriptionStatusName) {
234         final Map<String, Serializable> subscriptionDetailsAsMap =
235             Map.of(DATANODE_SELECTOR_LEAF_NAME, dataNodeSelector,
236                     DATAJOB_ID_LEAF_NAME, (Serializable) subscriptionIds,
237                     STATUS_LEAF_NAME, cmSubscriptionStatusName);
238         return "{\"subscription\":[" + jsonObjectMapper.asJsonString(subscriptionDetailsAsMap) + "]}";
239     }
240
241 }
242