Merge "Create datastores for NCMP w/subscription model"
[cps.git] / cps-service / src / main / java / org / onap / cps / api / impl / CpsDeltaServiceImpl.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 TechMahindra Ltd.
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.api.impl;
22
23
24 import java.io.Serializable;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.LinkedHashMap;
29 import java.util.List;
30 import java.util.Map;
31 import lombok.NoArgsConstructor;
32 import lombok.extern.slf4j.Slf4j;
33 import org.onap.cps.api.CpsDeltaService;
34 import org.onap.cps.spi.model.DataNode;
35 import org.onap.cps.spi.model.DeltaReport;
36 import org.onap.cps.spi.model.DeltaReportBuilder;
37 import org.springframework.stereotype.Service;
38
39 @Slf4j
40 @Service
41 @NoArgsConstructor
42 public class CpsDeltaServiceImpl implements CpsDeltaService {
43
44     @Override
45     public List<DeltaReport> getDeltaReports(final Collection<DataNode> sourceDataNodes,
46                                              final Collection<DataNode> targetDataNodes) {
47
48         final List<DeltaReport> deltaReport = new ArrayList<>();
49
50         final Map<String, DataNode> xpathToSourceDataNodes = convertToXPathToDataNodesMap(sourceDataNodes);
51         final Map<String, DataNode> xpathToTargetDataNodes = convertToXPathToDataNodesMap(targetDataNodes);
52
53         deltaReport.addAll(getRemovedDeltaReports(xpathToSourceDataNodes, xpathToTargetDataNodes));
54
55         deltaReport.addAll(getAddedDeltaReports(xpathToSourceDataNodes, xpathToTargetDataNodes));
56
57         return Collections.unmodifiableList(deltaReport);
58     }
59
60     private static Map<String, DataNode> convertToXPathToDataNodesMap(
61                                                                     final Collection<DataNode> dataNodes) {
62         final Map<String, DataNode> xpathToDataNode = new LinkedHashMap<>();
63         for (final DataNode dataNode : dataNodes) {
64             xpathToDataNode.put(dataNode.getXpath(), dataNode);
65             final Collection<DataNode> childDataNodes = dataNode.getChildDataNodes();
66             if (!childDataNodes.isEmpty()) {
67                 xpathToDataNode.putAll(convertToXPathToDataNodesMap(childDataNodes));
68             }
69         }
70         return xpathToDataNode;
71     }
72
73     private static List<DeltaReport> getRemovedDeltaReports(
74                                                             final Map<String, DataNode> xpathToSourceDataNodes,
75                                                             final Map<String, DataNode> xpathToTargetDataNodes) {
76
77         final List<DeltaReport> removedDeltaReportEntries = new ArrayList<>();
78         for (final Map.Entry<String, DataNode> entry: xpathToSourceDataNodes.entrySet()) {
79             final String xpath = entry.getKey();
80             final DataNode sourceDataNode = entry.getValue();
81             final DataNode targetDataNode = xpathToTargetDataNodes.get(xpath);
82
83             if (targetDataNode == null) {
84                 final Map<String, Serializable> sourceDataNodeLeaves = sourceDataNode.getLeaves();
85                 final DeltaReport removedData = new DeltaReportBuilder().actionRemove().withXpath(xpath)
86                         .withSourceData(sourceDataNodeLeaves).build();
87                 removedDeltaReportEntries.add(removedData);
88             }
89         }
90         return removedDeltaReportEntries;
91     }
92
93     private static List<DeltaReport> getAddedDeltaReports(final Map<String, DataNode> xpathToSourceDataNodes,
94                                                           final Map<String, DataNode> xpathToTargetDataNodes) {
95
96         final List<DeltaReport> addedDeltaReportEntries = new ArrayList<>();
97         final Map<String, DataNode> xpathToAddedNodes = new LinkedHashMap<>(xpathToTargetDataNodes);
98         xpathToAddedNodes.keySet().removeAll(xpathToSourceDataNodes.keySet());
99         for (final Map.Entry<String, DataNode> entry: xpathToAddedNodes.entrySet()) {
100             final String xpath = entry.getKey();
101             final DataNode dataNode = entry.getValue();
102             final DeltaReport addedDataForDeltaReport = new DeltaReportBuilder().actionAdd().withXpath(xpath)
103                                 .withTargetData(dataNode.getLeaves()).build();
104             addedDeltaReportEntries.add(addedDataForDeltaReport);
105         }
106         return addedDeltaReportEntries;
107     }
108 }