Add support for delete data-node event
[cps.git] / cps-service / src / main / java / org / onap / cps / api / impl / CpsDataServiceImpl.java
index a23bc95..af06e5f 100755 (executable)
@@ -24,6 +24,7 @@ package org.onap.cps.api.impl;
 
 import java.time.OffsetDateTime;
 import java.util.Collection;
+import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.onap.cps.api.CpsAdminService;
 import org.onap.cps.api.CpsDataService;
@@ -32,31 +33,25 @@ import org.onap.cps.notification.Operation;
 import org.onap.cps.spi.CpsDataPersistenceService;
 import org.onap.cps.spi.FetchDescendantsOption;
 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.utils.YangUtils;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 @Service
 @Slf4j
+@AllArgsConstructor
 public class CpsDataServiceImpl implements CpsDataService {
 
     private static final String ROOT_NODE_XPATH = "/";
 
-    @Autowired
-    private CpsDataPersistenceService cpsDataPersistenceService;
-
-    @Autowired
-    private CpsAdminService cpsAdminService;
-
-    @Autowired
-    private YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
-
-    @Autowired
-    private NotificationService notificationService;
+    private final CpsDataPersistenceService cpsDataPersistenceService;
+    private final CpsAdminService cpsAdminService;
+    private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
+    private final NotificationService notificationService;
 
     @Override
     public void saveData(final String dataspaceName, final String anchorName, final String jsonData,
@@ -137,6 +132,14 @@ public class CpsDataServiceImpl implements CpsDataService {
         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, dataNodeXpath, Operation.DELETE);
     }
 
+    @Override
+    public void deleteDataNodes(final String dataspaceName, final String anchorName,
+        final OffsetDateTime observedTimestamp) {
+        final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
+        cpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName);
+        processDataUpdatedEventAsync(anchor, ROOT_NODE_XPATH, Operation.DELETE, observedTimestamp);
+    }
+
     @Override
     public void deleteListOrListElement(final String dataspaceName, final String anchorName, final String listNodeXpath,
         final OffsetDateTime observedTimestamp) {
@@ -185,9 +188,16 @@ public class CpsDataServiceImpl implements CpsDataService {
     private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName,
                                               final OffsetDateTime observedTimestamp, final String xpath,
                                               final Operation operation) {
+        final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
+        this.processDataUpdatedEventAsync(anchor, xpath, operation, observedTimestamp);
+    }
+
+    private void processDataUpdatedEventAsync(final Anchor anchor, final String xpath, final Operation operation,
+        final OffsetDateTime observedTimestamp) {
         try {
-            notificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, xpath, operation);
+            notificationService.processDataUpdatedEvent(anchor, observedTimestamp, xpath, operation);
         } catch (final Exception exception) {
+            //If async message can't be queued for notification service, the initial request should not failed.
             log.error("Failed to send message to notification service", exception);
         }
     }