Merge "CPS-508: Create anchor/schemaset from new modules and existing modules"
[cps.git] / cps-service / src / main / java / org / onap / cps / api / impl / CpsDataServiceImpl.java
index fd0f76b..5e6e1a2 100755 (executable)
@@ -1,7 +1,7 @@
 /*
- * ============LICENSE_START=======================================================
+ *  ============LICENSE_START=======================================================
  *  Copyright (C) 2021 Nordix Foundation
- *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
+ *  Modifications Copyright (C) 2020-2021 Bell Canada.
  *  Modifications Copyright (C) 2021 Pantheon.tech
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
@@ -9,6 +9,7 @@
  *  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.
 
 package org.onap.cps.api.impl;
 
+import java.util.Collection;
 import org.onap.cps.api.CpsAdminService;
 import org.onap.cps.api.CpsDataService;
 import org.onap.cps.api.CpsModuleService;
+import org.onap.cps.notification.NotificationService;
 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.DataNode;
 import org.onap.cps.spi.model.DataNodeBuilder;
 import org.onap.cps.utils.YangUtils;
@@ -51,10 +55,14 @@ public class CpsDataServiceImpl implements CpsDataService {
     @Autowired
     private YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
 
+    @Autowired
+    private NotificationService notificationService;
+
     @Override
     public void saveData(final String dataspaceName, final String anchorName, final String jsonData) {
         final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData);
         cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode);
+        notificationService.processDataUpdatedEvent(dataspaceName, anchorName);
     }
 
     @Override
@@ -62,6 +70,16 @@ public class CpsDataServiceImpl implements CpsDataService {
         final String jsonData) {
         final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
         cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode);
+        notificationService.processDataUpdatedEvent(dataspaceName, anchorName);
+    }
+
+    @Override
+    public void saveListNodeData(final String dataspaceName, final String anchorName,
+        final String parentNodeXpath, final String jsonData) {
+        final Collection<DataNode> dataNodesCollection =
+            buildDataNodeCollectionFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
+        cpsDataPersistenceService.addListDataNodes(dataspaceName, anchorName, parentNodeXpath, dataNodesCollection);
+        notificationService.processDataUpdatedEvent(dataspaceName, anchorName);
     }
 
     @Override
@@ -76,6 +94,7 @@ public class CpsDataServiceImpl implements CpsDataService {
         final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
         cpsDataPersistenceService
             .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
+        notificationService.processDataUpdatedEvent(dataspaceName, anchorName);
     }
 
     @Override
@@ -83,8 +102,25 @@ public class CpsDataServiceImpl implements CpsDataService {
         final String jsonData) {
         final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
         cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
+        notificationService.processDataUpdatedEvent(dataspaceName, anchorName);
+    }
+
+    @Override
+    public void replaceListNodeData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
+        final String jsonData) {
+        final Collection<DataNode> dataNodes =
+            buildDataNodeCollectionFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
+        cpsDataPersistenceService.replaceListDataNodes(dataspaceName, anchorName, parentNodeXpath, dataNodes);
+        notificationService.processDataUpdatedEvent(dataspaceName, anchorName);
+    }
+
+    @Override
+    public void deleteListNodeData(final String dataspaceName, final String anchorName, final String listNodeXpath) {
+        cpsDataPersistenceService.deleteListDataNodes(dataspaceName, anchorName, listNodeXpath);
+        notificationService.processDataUpdatedEvent(dataspaceName, anchorName);
     }
 
+
     private DataNode buildDataNodeFromJson(final String dataspaceName, final String anchorName,
         final String parentNodeXpath, final String jsonData) {
 
@@ -103,7 +139,25 @@ public class CpsDataServiceImpl implements CpsDataService {
             .build();
     }
 
+    private Collection<DataNode> buildDataNodeCollectionFromJson(final String dataspaceName, final String anchorName,
+        final String parentNodeXpath, final String jsonData) {
+
+        final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
+        final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
+
+        final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
+        final Collection<DataNode> dataNodes = new DataNodeBuilder()
+            .withParentNodeXpath(parentNodeXpath)
+            .withNormalizedNodeTree(normalizedNode)
+            .buildCollection();
+        if (dataNodes.isEmpty()) {
+            throw new DataValidationException("Invalid list data.", "List node is empty.");
+        }
+        return dataNodes;
+
+    }
+
     private SchemaContext getSchemaContext(final String dataspaceName, final String schemaSetName) {
         return yangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName).getSchemaContext();
     }
-}
\ No newline at end of file
+}