b761f84257c5f4adc7a21b694d27e1eaf6c6c25b
[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.cmnotificationsubscription.utils;
23
24 import static org.onap.cps.api.parameters.FetchDescendantsOption.OMIT_DESCENDANTS;
25
26 import java.io.Serializable;
27 import java.time.OffsetDateTime;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.Map;
31 import lombok.RequiredArgsConstructor;
32 import lombok.extern.slf4j.Slf4j;
33 import org.onap.cps.api.CpsDataService;
34 import org.onap.cps.api.CpsQueryService;
35 import org.onap.cps.api.model.DataNode;
36 import org.onap.cps.utils.ContentType;
37 import org.onap.cps.utils.JsonObjectMapper;
38 import org.springframework.stereotype.Service;
39
40 @Slf4j
41 @Service
42 @RequiredArgsConstructor
43 public class CmDataJobSubscriptionPersistenceService {
44
45     private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
46     private static final String CM_DATA_JOB_SUBSCRIPTIONS_ANCHOR_NAME = "cm-data-job-subscriptions";
47     private static final String CM_DATA_JOB_SUBSCRIPTIONS_PARENT_NODE_XPATH = "/dataJob";
48     private static final String CPS_PATH_TEMPLATE_FOR_SUBSCRIPTION_WITH_ALTERNATE_ID_AND_DATATYPE =
49             "/dataJob/subscription[@alternateId='%s' and @dataTypeId='%s']";
50     private static final String CPS_PATH_TEMPLATE_FOR_SUBSCRIPTION_WITH_DATA_JOB_ID =
51             "//subscription/dataJobId[text()='%s']";
52
53     private final JsonObjectMapper jsonObjectMapper;
54     private final CpsQueryService cpsQueryService;
55     private final CpsDataService cpsDataService;
56
57     /**
58      * Check if we have a cm data job subscription for the given data type and target (FDN).
59      *
60      * @param dataType      the data type of the data job subscription
61      * @param alternateId   the alternate id target of the data job subscription
62      * @return              true if the subscription details has at least one subscriber , otherwise false
63      */
64     public boolean hasAtLeastOneSubscription(final String dataType, final String alternateId) {
65         return !getSubscriptionIds(dataType, alternateId).isEmpty();
66     }
67
68     /**
69      * Check if the input is a new subscription ID against ongoing subscriptions.
70      *
71      * @param subscriptionId subscription ID
72      * @return true if subscriptionId is not used in active subscriptions, otherwise false
73      */
74     public boolean isNewSubscriptionId(final String subscriptionId) {
75         final String query = CPS_PATH_TEMPLATE_FOR_SUBSCRIPTION_WITH_DATA_JOB_ID.formatted(subscriptionId);
76         return cpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, CM_DATA_JOB_SUBSCRIPTIONS_ANCHOR_NAME,
77                 query, OMIT_DESCENDANTS).isEmpty();
78     }
79
80     /**
81      * Get the ids for the subscriptions for the given data type and targets.
82      *
83      * @param dataType      the data type of the data job subscription
84      * @param alternateId   the alternate id target of the data job subscription
85      * @return              collection of subscription ids of ongoing cm notification subscription
86      */
87     @SuppressWarnings("unchecked")
88     public Collection<String> getSubscriptionIds(final String dataType, final String alternateId) {
89         final String query = CPS_PATH_TEMPLATE_FOR_SUBSCRIPTION_WITH_ALTERNATE_ID_AND_DATATYPE.formatted(
90                 alternateId, dataType);
91         final Collection<DataNode> existingNodes =
92                 cpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, CM_DATA_JOB_SUBSCRIPTIONS_ANCHOR_NAME,
93                         query, OMIT_DESCENDANTS);
94         if (existingNodes.isEmpty()) {
95             return Collections.emptyList();
96         }
97         return (Collection<String>) existingNodes.iterator().next().getLeaves().get("dataJobId");
98     }
99
100     /**
101      * Add cm notification data job subscription.
102      *
103      * @param dataType          the data type of the data job subscription
104      * @param alternateId       the alternate id target of the data job subscription
105      * @param subscriptionId data job subscription id to be added
106      */
107     public void addSubscription(final String dataType, final String alternateId, final String subscriptionId) {
108         final Collection<String> subscriptionIds = getSubscriptionIds(dataType, alternateId);
109         if (subscriptionIds.isEmpty()) {
110             addNewSubscriptionDetails(dataType, alternateId, subscriptionId);
111         } else {
112             subscriptionIds.add(subscriptionId);
113             updateSubscriptionDetails(subscriptionIds, dataType, alternateId);
114         }
115     }
116
117     /**
118      * Remove cm notification data job Subscription.
119      *
120      * @param dataType          the data type of the data job subscription
121      * @param alternateId       the alternate id target of the data job subscription
122      * @param subscriptionId    data subscription id to remove
123      */
124     public void removeSubscription(final String dataType, final String alternateId, final String subscriptionId) {
125         final Collection<String> subscriptionIds = getSubscriptionIds(dataType, alternateId);
126         if (subscriptionIds.remove(subscriptionId)) {
127             updateSubscriptionDetails(subscriptionIds, dataType, alternateId);
128             log.info("There is at least one subscriber left for dataType {} on {}", dataType, alternateId);
129             if (subscriptionIds.isEmpty()) {
130                 log.info("There are no subscribers left for dataType {} on {}", dataType, alternateId);
131                 deleteUnusedSubscriptionDetails(dataType, alternateId);
132             }
133         }
134     }
135
136     /**
137      * Retrieve all existing data nodes for given data job subscription id.
138      *
139      * @param subscriptionId  data job subscription id
140      * @return                collection of DataNodes
141      */
142     public Collection<DataNode> getAffectedDataNodes(final String subscriptionId) {
143         final String query = CPS_PATH_TEMPLATE_FOR_SUBSCRIPTION_WITH_DATA_JOB_ID.formatted(subscriptionId);
144         return cpsQueryService.queryDataNodes(NCMP_DATASPACE_NAME, CM_DATA_JOB_SUBSCRIPTIONS_ANCHOR_NAME,
145                 query, OMIT_DESCENDANTS);
146     }
147
148     private void deleteUnusedSubscriptionDetails(final String dataType, final String alternateId) {
149         final String deleteListOfSubscriptionCpsPathQuery =
150                 CPS_PATH_TEMPLATE_FOR_SUBSCRIPTION_WITH_ALTERNATE_ID_AND_DATATYPE.formatted(alternateId,
151                         dataType);
152         cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, CM_DATA_JOB_SUBSCRIPTIONS_ANCHOR_NAME,
153                 deleteListOfSubscriptionCpsPathQuery, OffsetDateTime.now());
154     }
155
156     private void addNewSubscriptionDetails(final String dataType,
157                                            final String alternateId,
158                                            final String subscriptionId) {
159         final Collection<String> newSubscriptionList = Collections.singletonList(subscriptionId);
160         final String subscriptionDetailsAsJson = getSubscriptionDetailsAsJson(newSubscriptionList, dataType,
161                 alternateId);
162         cpsDataService.saveData(NCMP_DATASPACE_NAME, CM_DATA_JOB_SUBSCRIPTIONS_ANCHOR_NAME, subscriptionDetailsAsJson,
163                 OffsetDateTime.now(), ContentType.JSON);
164     }
165
166     private void updateSubscriptionDetails(final Collection<String> subscriptionIds, final String dataType,
167                                            final String alternateId) {
168         final String subscriptionDetailsAsJson = getSubscriptionDetailsAsJson(subscriptionIds, dataType, alternateId);
169         cpsDataService.updateNodeLeaves(NCMP_DATASPACE_NAME, CM_DATA_JOB_SUBSCRIPTIONS_ANCHOR_NAME,
170                 CM_DATA_JOB_SUBSCRIPTIONS_PARENT_NODE_XPATH, subscriptionDetailsAsJson, OffsetDateTime.now(),
171                 ContentType.JSON);
172     }
173
174     private String getSubscriptionDetailsAsJson(final Collection<String> subscriptionIds,
175                                                 final String dataTypeId,
176                                                 final String alternateId) {
177         final Map<String, Serializable> subscriptionDetailsAsMap =
178                 Map.of("dataTypeId", dataTypeId,
179                         "alternateId", alternateId,
180                         "dataJobId", (Serializable) subscriptionIds);
181         return "{\"subscription\":[" + jsonObjectMapper.asJsonString(subscriptionDetailsAsMap) + "]}";
182     }
183
184 }
185