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
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.api.impl;
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;
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;
42 public class CpsDeltaServiceImpl implements CpsDeltaService {
45 public List<DeltaReport> getDeltaReports(final Collection<DataNode> sourceDataNodes,
46 final Collection<DataNode> targetDataNodes) {
48 final List<DeltaReport> deltaReport = new ArrayList<>();
50 final Map<String, DataNode> xpathToSourceDataNodes = convertToXPathToDataNodesMap(sourceDataNodes);
51 final Map<String, DataNode> xpathToTargetDataNodes = convertToXPathToDataNodesMap(targetDataNodes);
53 deltaReport.addAll(getRemovedDeltaReports(xpathToSourceDataNodes, xpathToTargetDataNodes));
55 deltaReport.addAll(getAddedDeltaReports(xpathToSourceDataNodes, xpathToTargetDataNodes));
57 return Collections.unmodifiableList(deltaReport);
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));
70 return xpathToDataNode;
73 private static List<DeltaReport> getRemovedDeltaReports(
74 final Map<String, DataNode> xpathToSourceDataNodes,
75 final Map<String, DataNode> xpathToTargetDataNodes) {
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);
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);
90 return removedDeltaReportEntries;
93 private static List<DeltaReport> getAddedDeltaReports(final Map<String, DataNode> xpathToSourceDataNodes,
94 final Map<String, DataNode> xpathToTargetDataNodes) {
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);
106 return addedDeltaReportEntries;