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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.impl.cmnotificationsubscription.utils;
24 import static org.onap.cps.api.parameters.FetchDescendantsOption.OMIT_DESCENDANTS;
26 import java.io.Serializable;
27 import java.time.OffsetDateTime;
28 import java.util.Collection;
29 import java.util.Collections;
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;
42 @RequiredArgsConstructor
43 public class CmDataJobSubscriptionPersistenceService {
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']";
53 private final JsonObjectMapper jsonObjectMapper;
54 private final CpsQueryService cpsQueryService;
55 private final CpsDataService cpsDataService;
58 * Check if we have a cm data job subscription for the given data type and target (FDN).
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
64 public boolean hasAtLeastOneSubscription(final String dataType, final String alternateId) {
65 return !getSubscriptionIds(dataType, alternateId).isEmpty();
69 * Check if the input is a new subscription ID against ongoing subscriptions.
71 * @param subscriptionId subscription ID
72 * @return true if subscriptionId is not used in active subscriptions, otherwise false
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();
81 * Get the ids for the subscriptions for the given data type and targets.
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
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();
97 return (Collection<String>) existingNodes.iterator().next().getLeaves().get("dataJobId");
101 * Add cm notification data job subscription.
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
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);
112 subscriptionIds.add(subscriptionId);
113 updateSubscriptionDetails(subscriptionIds, dataType, alternateId);
118 * Remove cm notification data job Subscription.
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
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);
137 * Retrieve all existing data nodes for given data job subscription id.
139 * @param subscriptionId data job subscription id
140 * @return collection of DataNodes
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);
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,
152 cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, CM_DATA_JOB_SUBSCRIPTIONS_ANCHOR_NAME,
153 deleteListOfSubscriptionCpsPathQuery, OffsetDateTime.now());
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,
162 cpsDataService.saveData(NCMP_DATASPACE_NAME, CM_DATA_JOB_SUBSCRIPTIONS_ANCHOR_NAME, subscriptionDetailsAsJson,
163 OffsetDateTime.now(), ContentType.JSON);
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(),
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) + "]}";