summaryrefslogtreecommitdiffstats
path: root/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/subscriptions/SubscriptionPersistenceImpl.java
blob: d2b1237a4d42dc43b0669a198b50f7de55b6702b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 *  ============LICENSE_START=======================================================
 *  Copyright (C) 2023 Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.onap.cps.ncmp.api.impl.subscriptions;

import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.onap.cps.api.CpsDataService;
import org.onap.cps.ncmp.api.impl.utils.DataNodeHelper;
import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent;
import org.onap.cps.spi.FetchDescendantsOption;
import org.onap.cps.spi.model.DataNode;
import org.onap.cps.utils.JsonObjectMapper;
import org.springframework.stereotype.Component;

@Slf4j
@RequiredArgsConstructor
@Component
public class SubscriptionPersistenceImpl implements SubscriptionPersistence {

    private static final String SUBSCRIPTION_DATASPACE_NAME = "NCMP-Admin";
    private static final String SUBSCRIPTION_ANCHOR_NAME = "AVC-Subscriptions";
    private static final String SUBSCRIPTION_REGISTRY_PARENT = "/subscription-registry";
    private final JsonObjectMapper jsonObjectMapper;
    private final CpsDataService cpsDataService;

    @Override
    public void saveSubscriptionEvent(final YangModelSubscriptionEvent yangModelSubscriptionEvent) {
        final String clientId = yangModelSubscriptionEvent.getClientId();
        final String subscriptionName = yangModelSubscriptionEvent.getSubscriptionName();

        final Collection<DataNode> dataNodes = cpsDataService.getDataNodes(SUBSCRIPTION_DATASPACE_NAME,
                SUBSCRIPTION_ANCHOR_NAME, SUBSCRIPTION_REGISTRY_PARENT, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);

        if (isSubscriptionRegistryEmptyOrNonExist(dataNodes, clientId, subscriptionName)) {
            saveSubscriptionEventYangModel(createSubscriptionEventJsonData(
                    jsonObjectMapper.asJsonString(yangModelSubscriptionEvent)));
        } else {
            findDeltaCmHandlesAddOrUpdateInDatabase(yangModelSubscriptionEvent, clientId, subscriptionName, dataNodes);
        }
    }

    private void findDeltaCmHandlesAddOrUpdateInDatabase(final YangModelSubscriptionEvent yangModelSubscriptionEvent,
                                                         final String clientId, final String subscriptionName,
                                                         final Collection<DataNode> dataNodes) {
        final Map<String, SubscriptionStatus> cmHandleIdsFromYangModel =
                extractCmHandleFromYangModelAsMap(yangModelSubscriptionEvent);
        final Map<String, SubscriptionStatus> cmHandleIdsFromDatabase =
                extractCmHandleFromDbAsMap(dataNodes);

        final Map<String, SubscriptionStatus> newCmHandles =
                mapDifference(cmHandleIdsFromYangModel, cmHandleIdsFromDatabase);
        traverseCmHandleList(newCmHandles, clientId, subscriptionName, true);

        final Map<String, SubscriptionStatus> existingCmHandles =
                mapDifference(cmHandleIdsFromYangModel, newCmHandles);
        traverseCmHandleList(existingCmHandles, clientId, subscriptionName, false);
    }

    private boolean isSubscriptionRegistryEmptyOrNonExist(final Collection<DataNode> dataNodes,
                                                          final String clientId, final String subscriptionName) {
        final Optional<DataNode> dataNodeFirst = dataNodes.stream().findFirst();
        return ((dataNodeFirst.isPresent() && dataNodeFirst.get().getChildDataNodes().isEmpty())
                || getCmHandlesForSubscriptionEvent(clientId, subscriptionName).isEmpty());
    }

    private void traverseCmHandleList(final Map<String, SubscriptionStatus> cmHandleMap,
                                      final String clientId,
                                      final String subscriptionName,
                                      final boolean isAddListElementOperation) {
        final List<YangModelSubscriptionEvent.TargetCmHandle> cmHandleList =
                targetCmHandlesAsList(cmHandleMap);
        for (final YangModelSubscriptionEvent.TargetCmHandle targetCmHandle : cmHandleList) {
            final String targetCmHandleAsJson =
                    createTargetCmHandleJsonData(jsonObjectMapper.asJsonString(targetCmHandle));
            addOrReplaceCmHandlePredicateListElement(targetCmHandleAsJson, clientId, subscriptionName,
                    isAddListElementOperation);
        }
    }

    private void addOrReplaceCmHandlePredicateListElement(final String targetCmHandleAsJson,
                                                          final String clientId,
                                                          final String subscriptionName,
                                                          final boolean isAddListElementOperation) {
        if (isAddListElementOperation) {
            log.info("targetCmHandleAsJson to be added into DB {}", targetCmHandleAsJson);
            cpsDataService.saveListElements(SUBSCRIPTION_DATASPACE_NAME,
                    SUBSCRIPTION_ANCHOR_NAME, createCmHandleXpathPredicates(clientId, subscriptionName),
                    targetCmHandleAsJson, NO_TIMESTAMP);
        } else {
            log.info("targetCmHandleAsJson to be updated into DB {}", targetCmHandleAsJson);
            cpsDataService.updateNodeLeaves(SUBSCRIPTION_DATASPACE_NAME,
                    SUBSCRIPTION_ANCHOR_NAME, createCmHandleXpathPredicates(clientId, subscriptionName),
                    targetCmHandleAsJson, NO_TIMESTAMP);
        }
    }

    private void saveSubscriptionEventYangModel(final String subscriptionEventJsonData) {
        log.info("SubscriptionEventJsonData to be saved into DB {}", subscriptionEventJsonData);
        cpsDataService.saveListElements(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME,
                SUBSCRIPTION_REGISTRY_PARENT, subscriptionEventJsonData, NO_TIMESTAMP);
    }

    @Override
    public Collection<DataNode> getDataNodesForSubscriptionEvent() {
        return cpsDataService.getDataNodes(SUBSCRIPTION_DATASPACE_NAME,
                SUBSCRIPTION_ANCHOR_NAME, SUBSCRIPTION_REGISTRY_PARENT,
                FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
    }

    @Override
    public Collection<DataNode> getCmHandlesForSubscriptionEvent(final String clientId, final String subscriptionName) {
        return cpsDataService.getDataNodesForMultipleXpaths(SUBSCRIPTION_DATASPACE_NAME,
                SUBSCRIPTION_ANCHOR_NAME, Arrays.asList(createCmHandleXpath(clientId, subscriptionName)),
                FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
    }

    private static Map<String, SubscriptionStatus> extractCmHandleFromDbAsMap(final Collection<DataNode> dataNodes) {
        final List<Map<String, Serializable>> dataNodeLeaves = DataNodeHelper.getDataNodeLeaves(dataNodes);
        final List<Collection<Serializable>> cmHandleIdToStatus = DataNodeHelper.getCmHandleIdToStatus(dataNodeLeaves);
        return DataNodeHelper.getCmHandleIdToStatusMap(cmHandleIdToStatus);
    }

    private static Map<String, SubscriptionStatus> extractCmHandleFromYangModelAsMap(
            final YangModelSubscriptionEvent yangModelSubscriptionEvent) {
        return yangModelSubscriptionEvent.getPredicates().getTargetCmHandles()
                .stream().collect(Collectors.toMap(
                        YangModelSubscriptionEvent.TargetCmHandle::getCmHandleId,
                        YangModelSubscriptionEvent.TargetCmHandle::getStatus));
    }

    private static List<YangModelSubscriptionEvent.TargetCmHandle> targetCmHandlesAsList(
            final Map<String, SubscriptionStatus> newCmHandles) {
        return newCmHandles.entrySet().stream().map(entry ->
                new YangModelSubscriptionEvent.TargetCmHandle(entry.getKey(),
                        entry.getValue())).collect(Collectors.toList());
    }

    private static String createSubscriptionEventJsonData(final String yangModelSubscriptionAsJson) {
        return "{\"subscription\":[" + yangModelSubscriptionAsJson + "]}";
    }

    private static String createTargetCmHandleJsonData(final String targetCmHandleAsJson) {
        return "{\"targetCmHandles\":[" + targetCmHandleAsJson + "]}";
    }

    private static String createCmHandleXpathPredicates(final String clientId, final String subscriptionName) {
        return "/subscription-registry/subscription[@clientID='" + clientId
                + "' and @subscriptionName='" + subscriptionName + "']/predicates";
    }

    private static String createCmHandleXpath(final String clientId, final String subscriptionName) {
        return "/subscription-registry/subscription[@clientID='" + clientId
                + "' and @subscriptionName='" + subscriptionName + "']";
    }

    private static <K, V> Map<K, V> mapDifference(final Map<? extends K, ? extends V> left,
                                                  final Map<? extends K, ? extends V> right) {
        final Map<K, V> difference = new HashMap<>();
        difference.putAll(left);
        difference.putAll(right);
        difference.entrySet().removeAll(right.entrySet());
        return difference;
    }
}