From 0339c71815a4ca4cbab3d263d6c4586a112cda64 Mon Sep 17 00:00:00 2001 From: Arpit Singh Date: Wed, 2 Aug 2023 18:35:31 +0530 Subject: CPS Delta API 1: Delta between 2 anchors - CPS Delta Feature Part 1: To find delta between two anchors - created new endpoint deltaByDataspaceAndAnchors - endpoint to take dataspaceName, source anchor, target anchor, xpath, descendants as input - added new service CpsDeltaService - added method to find delta between DataNodes: getDeltaReport - added method to find removed data nodes: getRemovedDeltaReports - added method to get Added DataNodes: getAddedDeltaReports - added method to get Map of xpath to DataNode: convertToXPathToDataNodesMap - added a POJO for delta report - Added new JSON data for delta feature testing - Added groovy test files CpsDeltaServiceImplSpec and DeltaReportBuilderSpec - code related to update operation, will be added in separate commit Issue-ID: CPS-1824 Signed-off-by: Arpit Singh Change-Id: I313f0f71d04b03878be7643f709d8af1aa6df6ba --- .../main/java/org/onap/cps/api/CpsDataService.java | 17 ++++ .../java/org/onap/cps/api/CpsDeltaService.java | 42 ++++++++ .../org/onap/cps/api/impl/CpsDataServiceImpl.java | 20 ++++ .../org/onap/cps/api/impl/CpsDeltaServiceImpl.java | 108 +++++++++++++++++++++ .../java/org/onap/cps/spi/model/DeltaReport.java | 42 ++++++++ .../org/onap/cps/spi/model/DeltaReportBuilder.java | 79 +++++++++++++++ 6 files changed, 308 insertions(+) create mode 100644 cps-service/src/main/java/org/onap/cps/api/CpsDeltaService.java create mode 100644 cps-service/src/main/java/org/onap/cps/api/impl/CpsDeltaServiceImpl.java create mode 100644 cps-service/src/main/java/org/onap/cps/spi/model/DeltaReport.java create mode 100644 cps-service/src/main/java/org/onap/cps/spi/model/DeltaReportBuilder.java (limited to 'cps-service/src/main') diff --git a/cps-service/src/main/java/org/onap/cps/api/CpsDataService.java b/cps-service/src/main/java/org/onap/cps/api/CpsDataService.java index 6a2cac4679..c9879595a8 100644 --- a/cps-service/src/main/java/org/onap/cps/api/CpsDataService.java +++ b/cps-service/src/main/java/org/onap/cps/api/CpsDataService.java @@ -26,9 +26,11 @@ package org.onap.cps.api; import java.time.OffsetDateTime; import java.util.Collection; +import java.util.List; import java.util.Map; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.model.DataNode; +import org.onap.cps.spi.model.DeltaReport; import org.onap.cps.utils.ContentType; /* @@ -298,4 +300,19 @@ public interface CpsDataService { * @param timeoutInMilliseconds lock attempt timeout in milliseconds */ void lockAnchor(String sessionID, String dataspaceName, String anchorName, Long timeoutInMilliseconds); + + /** + * Retrieves the delta between two anchors by xpath within a dataspace. + * + * @param dataspaceName dataspace name + * @param sourceAnchorName source anchor name + * @param targetAnchorName target anchor name + * @param xpath xpath + * @param fetchDescendantsOption defines the scope of data to fetch: either single node or all the descendant + * nodes (recursively) as well + * @return list containing {@link DeltaReport} objects + */ + List getDeltaByDataspaceAndAnchors(String dataspaceName, String sourceAnchorName, + String targetAnchorName, String xpath, + FetchDescendantsOption fetchDescendantsOption); } diff --git a/cps-service/src/main/java/org/onap/cps/api/CpsDeltaService.java b/cps-service/src/main/java/org/onap/cps/api/CpsDeltaService.java new file mode 100644 index 0000000000..d806c208aa --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/api/CpsDeltaService.java @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 TechMahindra Ltd. + * ================================================================================ + * 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.api; + +import java.util.Collection; +import java.util.List; +import org.onap.cps.spi.model.DataNode; +import org.onap.cps.spi.model.DeltaReport; + +public interface CpsDeltaService { + + /** + * Retrieves delta between source data nodes and target data nodes. Source data nodes contain the data which acts as + * the point of reference for delta report, whereas target data nodes contain the data being compared against + * source data node. List of {@link DeltaReport}. Each Delta Report contains information such as action, xpath, + * source-payload and target-payload. + * + * @param sourceDataNodes collection of {@link DataNode} as source/reference for delta generation + * @param targetDataNodes collection of {@link DataNode} as target data for delta generation + * @return list of {@link DeltaReport} containing delta information + */ + List getDeltaReports(Collection sourceDataNodes, + Collection targetDataNodes); +} diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java b/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java index 1d68450f8a..e74e0ad249 100755 --- a/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java +++ b/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java @@ -34,12 +34,14 @@ import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.api.CpsAdminService; import org.onap.cps.api.CpsDataService; +import org.onap.cps.api.CpsDeltaService; import org.onap.cps.cpspath.parser.CpsPathUtil; import org.onap.cps.notification.NotificationService; import org.onap.cps.notification.Operation; @@ -49,6 +51,7 @@ import org.onap.cps.spi.exceptions.DataValidationException; import org.onap.cps.spi.model.Anchor; import org.onap.cps.spi.model.DataNode; import org.onap.cps.spi.model.DataNodeBuilder; +import org.onap.cps.spi.model.DeltaReport; import org.onap.cps.spi.utils.CpsValidator; import org.onap.cps.utils.ContentType; import org.onap.cps.utils.TimedYangParser; @@ -70,6 +73,7 @@ public class CpsDataServiceImpl implements CpsDataService { private final NotificationService notificationService; private final CpsValidator cpsValidator; private final TimedYangParser timedYangParser; + private final CpsDeltaService cpsDeltaService; @Override public void saveData(final String dataspaceName, final String anchorName, final String nodeData, @@ -214,6 +218,22 @@ public class CpsDataServiceImpl implements CpsDataService { cpsDataPersistenceService.lockAnchor(sessionID, dataspaceName, anchorName, timeoutInMilliseconds); } + @Override + @Timed(value = "cps.data.service.get.delta", + description = "Time taken to get delta between anchors") + public List getDeltaByDataspaceAndAnchors(final String dataspaceName, + final String sourceAnchorName, + final String targetAnchorName, final String xpath, + final FetchDescendantsOption fetchDescendantsOption) { + + final Collection sourceDataNodes = getDataNodesForMultipleXpaths(dataspaceName, + sourceAnchorName, Collections.singletonList(xpath), fetchDescendantsOption); + final Collection targetDataNodes = getDataNodesForMultipleXpaths(dataspaceName, + targetAnchorName, Collections.singletonList(xpath), fetchDescendantsOption); + + return cpsDeltaService.getDeltaReports(sourceDataNodes, targetDataNodes); + } + @Override @Timed(value = "cps.data.service.datanode.descendants.update", description = "Time taken to update a data node and descendants") diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/CpsDeltaServiceImpl.java b/cps-service/src/main/java/org/onap/cps/api/impl/CpsDeltaServiceImpl.java new file mode 100644 index 0000000000..683ddce3d1 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/api/impl/CpsDeltaServiceImpl.java @@ -0,0 +1,108 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 TechMahindra Ltd. + * ================================================================================ + * 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.api.impl; + + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.api.CpsDeltaService; +import org.onap.cps.spi.model.DataNode; +import org.onap.cps.spi.model.DeltaReport; +import org.onap.cps.spi.model.DeltaReportBuilder; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +@NoArgsConstructor +public class CpsDeltaServiceImpl implements CpsDeltaService { + + @Override + public List getDeltaReports(final Collection sourceDataNodes, + final Collection targetDataNodes) { + + final List deltaReport = new ArrayList<>(); + + final Map xpathToSourceDataNodes = convertToXPathToDataNodesMap(sourceDataNodes); + final Map xpathToTargetDataNodes = convertToXPathToDataNodesMap(targetDataNodes); + + deltaReport.addAll(getRemovedDeltaReports(xpathToSourceDataNodes, xpathToTargetDataNodes)); + + deltaReport.addAll(getAddedDeltaReports(xpathToSourceDataNodes, xpathToTargetDataNodes)); + + return Collections.unmodifiableList(deltaReport); + } + + private static Map convertToXPathToDataNodesMap( + final Collection dataNodes) { + final Map xpathToDataNode = new LinkedHashMap<>(); + for (final DataNode dataNode : dataNodes) { + xpathToDataNode.put(dataNode.getXpath(), dataNode); + final Collection childDataNodes = dataNode.getChildDataNodes(); + if (!childDataNodes.isEmpty()) { + xpathToDataNode.putAll(convertToXPathToDataNodesMap(childDataNodes)); + } + } + return xpathToDataNode; + } + + private static List getRemovedDeltaReports( + final Map xpathToSourceDataNodes, + final Map xpathToTargetDataNodes) { + + final List removedDeltaReportEntries = new ArrayList<>(); + for (final Map.Entry entry: xpathToSourceDataNodes.entrySet()) { + final String xpath = entry.getKey(); + final DataNode sourceDataNode = entry.getValue(); + final DataNode targetDataNode = xpathToTargetDataNodes.get(xpath); + + if (targetDataNode == null) { + final Map sourceDataNodeLeaves = sourceDataNode.getLeaves(); + final DeltaReport removedData = new DeltaReportBuilder().actionRemove().withXpath(xpath) + .withSourceData(sourceDataNodeLeaves).build(); + removedDeltaReportEntries.add(removedData); + } + } + return removedDeltaReportEntries; + } + + private static List getAddedDeltaReports(final Map xpathToSourceDataNodes, + final Map xpathToTargetDataNodes) { + + final List addedDeltaReportEntries = new ArrayList<>(); + final Map xpathToAddedNodes = new LinkedHashMap<>(xpathToTargetDataNodes); + xpathToAddedNodes.keySet().removeAll(xpathToSourceDataNodes.keySet()); + for (final Map.Entry entry: xpathToAddedNodes.entrySet()) { + final String xpath = entry.getKey(); + final DataNode dataNode = entry.getValue(); + final DeltaReport addedDataForDeltaReport = new DeltaReportBuilder().actionAdd().withXpath(xpath) + .withTargetData(dataNode.getLeaves()).build(); + addedDeltaReportEntries.add(addedDataForDeltaReport); + } + return addedDeltaReportEntries; + } +} diff --git a/cps-service/src/main/java/org/onap/cps/spi/model/DeltaReport.java b/cps-service/src/main/java/org/onap/cps/spi/model/DeltaReport.java new file mode 100644 index 0000000000..b9c05dcf02 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/spi/model/DeltaReport.java @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 TechMahindra Ltd. + * ================================================================================ + * 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.spi.model; + +import java.io.Serializable; +import java.util.Map; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; + +@Setter(AccessLevel.PROTECTED) +@Getter +public class DeltaReport { + + public static final String ADD_ACTION = "add"; + public static final String REMOVE_ACTION = "remove"; + + DeltaReport() {} + + private String action; + private String xpath; + private Map sourceData; + private Map targetData; +} diff --git a/cps-service/src/main/java/org/onap/cps/spi/model/DeltaReportBuilder.java b/cps-service/src/main/java/org/onap/cps/spi/model/DeltaReportBuilder.java new file mode 100644 index 0000000000..cef6ca3fa2 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/spi/model/DeltaReportBuilder.java @@ -0,0 +1,79 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 TechMahindra Ltd. + * ================================================================================ + * 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.spi.model; + +import java.io.Serializable; +import java.util.Map; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class DeltaReportBuilder { + + + private String action; + private String xpath; + private Map sourceData; + private Map targetData; + + public DeltaReportBuilder withXpath(final String xpath) { + this.xpath = xpath; + return this; + } + + public DeltaReportBuilder withSourceData(final Map sourceData) { + this.sourceData = sourceData; + return this; + } + + public DeltaReportBuilder withTargetData(final Map targetData) { + this.targetData = targetData; + return this; + } + + public DeltaReportBuilder actionAdd() { + this.action = DeltaReport.ADD_ACTION; + return this; + } + + public DeltaReportBuilder actionRemove() { + this.action = DeltaReport.REMOVE_ACTION; + return this; + } + + /** + * To create a single entry of {@link DeltaReport}. + * + * @return {@link DeltaReport} + */ + public DeltaReport build() { + final DeltaReport deltaReport = new DeltaReport(); + deltaReport.setAction(action); + deltaReport.setXpath(xpath); + if (sourceData != null && !sourceData.isEmpty()) { + deltaReport.setSourceData(sourceData); + } + + if (targetData != null && !targetData.isEmpty()) { + deltaReport.setTargetData(targetData); + } + return deltaReport; + } +} -- cgit 1.2.3-korg