Persisting a list element to a parent list (ep2)
[cps.git] / cps-service / src / main / java / org / onap / cps / api / impl / CpsDataServiceImpl.java
index 99cda22..7db87e8 100755 (executable)
@@ -39,6 +39,7 @@ import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.onap.cps.api.CpsAdminService;
 import org.onap.cps.api.CpsDataService;
+import org.onap.cps.cpspath.parser.CpsPathUtil;
 import org.onap.cps.notification.NotificationService;
 import org.onap.cps.notification.Operation;
 import org.onap.cps.spi.CpsDataPersistenceService;
@@ -115,8 +116,12 @@ public class CpsDataServiceImpl implements CpsDataService {
         final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
         final Collection<DataNode> listElementDataNodeCollection =
             buildDataNodes(anchor, parentNodeXpath, jsonData, ContentType.JSON);
-        cpsDataPersistenceService.addListElements(dataspaceName, anchorName, parentNodeXpath,
-            listElementDataNodeCollection);
+        if (isRootNodeXpath(parentNodeXpath)) {
+            cpsDataPersistenceService.storeDataNodes(dataspaceName, anchorName, listElementDataNodeCollection);
+        } else {
+            cpsDataPersistenceService.addListElements(dataspaceName, anchorName, parentNodeXpath,
+                                                      listElementDataNodeCollection);
+        }
         processDataUpdatedEventAsync(anchor, parentNodeXpath, UPDATE, observedTimestamp);
     }
 
@@ -350,18 +355,19 @@ public class CpsDataServiceImpl implements CpsDataService {
                     .withContainerNode(containerNode)
                     .buildCollection();
             if (dataNodes.isEmpty()) {
-                throw new DataValidationException("Invalid data.", "No data nodes provided");
+                throw new DataValidationException("No data nodes.", "No data nodes provided");
             }
             return dataNodes;
         }
+        final String normalizedParentNodeXpath = CpsPathUtil.getNormalizedXpath(parentNodeXpath);
         final ContainerNode containerNode =
-            timedYangParser.parseData(contentType, nodeData, schemaContext, parentNodeXpath);
+            timedYangParser.parseData(contentType, nodeData, schemaContext, normalizedParentNodeXpath);
         final Collection<DataNode> dataNodes = new DataNodeBuilder()
-            .withParentNodeXpath(parentNodeXpath)
+            .withParentNodeXpath(normalizedParentNodeXpath)
             .withContainerNode(containerNode)
             .buildCollection();
         if (dataNodes.isEmpty()) {
-            throw new DataValidationException("Invalid data.", "No data nodes provided");
+            throw new DataValidationException("No data nodes.", "No data nodes provided");
         }
         return dataNodes;
     }
@@ -379,7 +385,7 @@ public class CpsDataServiceImpl implements CpsDataService {
         try {
             notificationService.processDataUpdatedEvent(anchor, xpath, operation, observedTimestamp);
         } catch (final Exception exception) {
-            //If async message can't be queued for notification service, the initial request should not failed.
+            //If async message can't be queued for notification service, the initial request should not fail.
             log.error("Failed to send message to notification service", exception);
         }
     }
@@ -389,10 +395,11 @@ public class CpsDataServiceImpl implements CpsDataService {
             .get(anchor.getDataspaceName(), anchor.getSchemaSetName()).getSchemaContext();
     }
 
+    private static boolean isRootNodeXpath(final String xpath) {
+        return ROOT_NODE_XPATH.equals(xpath);
+    }
+
     private void processDataNodeUpdate(final Anchor anchor, final DataNode dataNodeUpdate) {
-        if (dataNodeUpdate == null) {
-            return;
-        }
         cpsDataPersistenceService.batchUpdateDataLeaves(anchor.getDataspaceName(), anchor.getName(),
                 Collections.singletonMap(dataNodeUpdate.getXpath(), dataNodeUpdate.getLeaves()));
         final Collection<DataNode> childDataNodeUpdates = dataNodeUpdate.getChildDataNodes();