2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.subscriptions;
23 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.Optional;
31 import java.util.stream.Collectors;
32 import lombok.RequiredArgsConstructor;
33 import lombok.extern.slf4j.Slf4j;
34 import org.onap.cps.api.CpsDataService;
35 import org.onap.cps.ncmp.api.impl.utils.DataNodeHelper;
36 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent;
37 import org.onap.cps.spi.FetchDescendantsOption;
38 import org.onap.cps.spi.model.DataNode;
39 import org.onap.cps.utils.JsonObjectMapper;
40 import org.springframework.stereotype.Component;
43 @RequiredArgsConstructor
45 public class SubscriptionPersistenceImpl implements SubscriptionPersistence {
47 private static final String SUBSCRIPTION_DATASPACE_NAME = "NCMP-Admin";
48 private static final String SUBSCRIPTION_ANCHOR_NAME = "AVC-Subscriptions";
49 private static final String SUBSCRIPTION_REGISTRY_PARENT = "/subscription-registry";
50 private final JsonObjectMapper jsonObjectMapper;
51 private final CpsDataService cpsDataService;
54 public void saveSubscriptionEvent(final YangModelSubscriptionEvent yangModelSubscriptionEvent) {
55 final String clientId = yangModelSubscriptionEvent.getClientId();
56 final String subscriptionName = yangModelSubscriptionEvent.getSubscriptionName();
58 final Collection<DataNode> dataNodes = cpsDataService.getDataNodes(SUBSCRIPTION_DATASPACE_NAME,
59 SUBSCRIPTION_ANCHOR_NAME, SUBSCRIPTION_REGISTRY_PARENT, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
61 if (isSubscriptionRegistryEmptyOrNonExist(dataNodes, clientId, subscriptionName)) {
62 saveSubscriptionEventYangModel(createSubscriptionEventJsonData(
63 jsonObjectMapper.asJsonString(yangModelSubscriptionEvent)));
65 findDeltaCmHandlesAddOrUpdateInDatabase(yangModelSubscriptionEvent, clientId, subscriptionName, dataNodes);
69 private void findDeltaCmHandlesAddOrUpdateInDatabase(final YangModelSubscriptionEvent yangModelSubscriptionEvent,
70 final String clientId, final String subscriptionName,
71 final Collection<DataNode> dataNodes) {
72 final Map<String, Map<String, String>> cmHandleIdToStatusAndDetailsAsMapNew =
73 extractCmHandleFromYangModelAsMap(yangModelSubscriptionEvent);
74 final Map<String, Map<String, String>> cmHandleIdToStatusAndDetailsAsMapOriginal =
75 DataNodeHelper.cmHandleIdToStatusAndDetailsAsMapFromDataNode(dataNodes);
77 final Map<String, Map<String, String>> newTargetCmHandles =
78 mapDifference(cmHandleIdToStatusAndDetailsAsMapNew,
79 cmHandleIdToStatusAndDetailsAsMapOriginal);
80 traverseCmHandleList(newTargetCmHandles, clientId, subscriptionName, true);
82 final Map<String, Map<String, String>> existingTargetCmHandles =
83 mapDifference(cmHandleIdToStatusAndDetailsAsMapNew, newTargetCmHandles);
84 traverseCmHandleList(existingTargetCmHandles, clientId, subscriptionName, false);
87 private static Map<String, Map<String, String>> extractCmHandleFromYangModelAsMap(
88 final YangModelSubscriptionEvent yangModelSubscriptionEvent) {
89 return yangModelSubscriptionEvent.getPredicates().getTargetCmHandles()
91 HashMap<String, Map<String, String>>::new,
92 (result, cmHandle) -> {
93 final String cmHandleId = cmHandle.getCmHandleId();
94 final SubscriptionStatus status = cmHandle.getStatus();
95 final String details = cmHandle.getDetails();
97 if (cmHandleId != null && status != null) {
98 result.put(cmHandleId, new HashMap<>());
99 result.get(cmHandleId).put("status", status.toString());
100 result.get(cmHandleId).put("details", details == null ? "" : details);
107 private void traverseCmHandleList(final Map<String, Map<String, String>> cmHandleMap,
108 final String clientId,
109 final String subscriptionName,
110 final boolean isAddListElementOperation) {
111 final List<YangModelSubscriptionEvent.TargetCmHandle> cmHandleList = targetCmHandlesAsList(cmHandleMap);
112 for (final YangModelSubscriptionEvent.TargetCmHandle targetCmHandle : cmHandleList) {
113 final String targetCmHandleAsJson =
114 createTargetCmHandleJsonData(jsonObjectMapper.asJsonString(targetCmHandle));
115 addOrReplaceCmHandlePredicateListElement(targetCmHandleAsJson, clientId, subscriptionName,
116 isAddListElementOperation);
120 private boolean isSubscriptionRegistryEmptyOrNonExist(final Collection<DataNode> dataNodes,
121 final String clientId, final String subscriptionName) {
122 final Optional<DataNode> dataNodeFirst = dataNodes.stream().findFirst();
123 return ((dataNodeFirst.isPresent() && dataNodeFirst.get().getChildDataNodes().isEmpty())
124 || getCmHandlesForSubscriptionEvent(clientId, subscriptionName).isEmpty());
127 private void addOrReplaceCmHandlePredicateListElement(final String targetCmHandleAsJson,
128 final String clientId,
129 final String subscriptionName,
130 final boolean isAddListElementOperation) {
131 if (isAddListElementOperation) {
132 log.info("targetCmHandleAsJson to be added into DB {}", targetCmHandleAsJson);
133 cpsDataService.saveListElements(SUBSCRIPTION_DATASPACE_NAME,
134 SUBSCRIPTION_ANCHOR_NAME, createCmHandleXpathPredicates(clientId, subscriptionName),
135 targetCmHandleAsJson, NO_TIMESTAMP);
137 log.info("targetCmHandleAsJson to be updated into DB {}", targetCmHandleAsJson);
138 cpsDataService.updateNodeLeaves(SUBSCRIPTION_DATASPACE_NAME,
139 SUBSCRIPTION_ANCHOR_NAME, createCmHandleXpathPredicates(clientId, subscriptionName),
140 targetCmHandleAsJson, NO_TIMESTAMP);
144 private void saveSubscriptionEventYangModel(final String subscriptionEventJsonData) {
145 log.info("SubscriptionEventJsonData to be saved into DB {}", subscriptionEventJsonData);
146 cpsDataService.saveListElements(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME,
147 SUBSCRIPTION_REGISTRY_PARENT, subscriptionEventJsonData, NO_TIMESTAMP);
151 public Collection<DataNode> getDataNodesForSubscriptionEvent() {
152 return cpsDataService.getDataNodes(SUBSCRIPTION_DATASPACE_NAME,
153 SUBSCRIPTION_ANCHOR_NAME, SUBSCRIPTION_REGISTRY_PARENT,
154 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
158 public Collection<DataNode> getCmHandlesForSubscriptionEvent(final String clientId, final String subscriptionName) {
159 return cpsDataService.getDataNodesForMultipleXpaths(SUBSCRIPTION_DATASPACE_NAME,
160 SUBSCRIPTION_ANCHOR_NAME, Arrays.asList(createCmHandleXpath(clientId, subscriptionName)),
161 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
164 private static List<YangModelSubscriptionEvent.TargetCmHandle> targetCmHandlesAsList(
165 final Map<String, Map<String, String>> newCmHandles) {
166 return newCmHandles.entrySet().stream().map(entry -> {
167 final String cmHandleId = entry.getKey();
168 final Map<String, String> statusAndDetailsMap = entry.getValue();
169 final String status = statusAndDetailsMap.get("status");
170 final String details = statusAndDetailsMap.get("details");
171 return new YangModelSubscriptionEvent.TargetCmHandle(cmHandleId,
172 SubscriptionStatus.fromString(status), details);
173 }).collect(Collectors.toList());
176 private static String createSubscriptionEventJsonData(final String yangModelSubscriptionAsJson) {
177 return "{\"subscription\":[" + yangModelSubscriptionAsJson + "]}";
180 private static String createTargetCmHandleJsonData(final String targetCmHandleAsJson) {
181 return "{\"targetCmHandles\":[" + targetCmHandleAsJson + "]}";
184 private static String createCmHandleXpathPredicates(final String clientId, final String subscriptionName) {
185 return "/subscription-registry/subscription[@clientID='" + clientId
186 + "' and @subscriptionName='" + subscriptionName + "']/predicates";
189 private static String createCmHandleXpath(final String clientId, final String subscriptionName) {
190 return "/subscription-registry/subscription[@clientID='" + clientId
191 + "' and @subscriptionName='" + subscriptionName + "']";
194 private static <K, L, M> Map<K, Map<L, M>> mapDifference(final Map<K, Map<L, M>> left,
195 final Map<K, Map<L, M>> right) {
196 final Map<K, Map<L, M>> difference = new HashMap<>();
197 difference.putAll(left);
198 difference.putAll(right);
199 difference.entrySet().removeAll(right.entrySet());