subscription-registry node in subscription loader 33/133633/2
authorlukegleeson <luke.gleeson@est.tech>
Mon, 13 Mar 2023 15:00:19 +0000 (15:00 +0000)
committerlukegleeson <luke.gleeson@est.tech>
Tue, 14 Mar 2023 12:05:23 +0000 (12:05 +0000)
Adding top node subscription-registry for the adding of subscriptions

Issue-ID: CPS-1548
Signed-off-by: lukegleeson <luke.gleeson@est.tech>
Change-Id: I4107293ce023e4c53dbb8b5d3feb0922cc3d4817

cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/SubscriptionModelLoader.java
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/SubscriptionModelLoaderSpec.groovy

index 0d82bb5..705c9d2 100644 (file)
@@ -22,11 +22,13 @@ package org.onap.cps.ncmp.init;
 
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
+import java.time.OffsetDateTime;
 import java.util.Map;
 import lombok.NonNull;
 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.CpsModuleService;
 import org.onap.cps.ncmp.api.impl.exception.NcmpStartUpException;
 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
@@ -42,9 +44,11 @@ public class SubscriptionModelLoader implements ModelLoader {
 
     private final CpsAdminService cpsAdminService;
     private final CpsModuleService cpsModuleService;
+    private final CpsDataService cpsDataService;
     private static final String SUBSCRIPTION_DATASPACE_NAME = "NCMP-Admin";
     private static final String SUBSCRIPTION_ANCHOR_NAME = "AVC-Subscriptions";
     private static final String SUBSCRIPTION_SCHEMASET_NAME = "subscriptions";
+    private static final String SUBSCRIPTION_REGISTRY_DATANODE_NAME = "subscription-registry";
 
     @Value("${ncmp.model-loader.subscription:false}")
     private boolean subscriptionModelLoaderEnabled;
@@ -76,6 +80,8 @@ public class SubscriptionModelLoader implements ModelLoader {
         if (!yangResourceContentMap.get("subscription.yang").isEmpty()) {
             createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, yangResourceContentMap);
             createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME);
+            createTopLevelDataNode(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME,
+                SUBSCRIPTION_REGISTRY_DATANODE_NAME);
         }
     }
 
@@ -116,6 +122,20 @@ public class SubscriptionModelLoader implements ModelLoader {
         return true;
     }
 
+    private void createTopLevelDataNode(final String dataspaceName,
+                                        final String anchorName,
+                                        final String dataNodeName) {
+        final String nodeData = "{\"" + dataNodeName + "\":{}}";
+        try {
+            cpsDataService.saveData(dataspaceName, anchorName, nodeData, OffsetDateTime.now());
+        } catch (final AlreadyDefinedException exception) {
+            log.info("Creating new data node {} failed as data node already exists", dataNodeName);
+        } catch (final Exception exception) {
+            log.debug("Creating data node for subscription model failed: {}", exception.getMessage());
+            throw new NcmpStartUpException("Creating data node failed", exception.getMessage());
+        }
+    }
+
     private String getFileContentAsString() {
         try (InputStream inputStream = getClass().getClassLoader()
                 .getResourceAsStream("model/subscription.yang")) {
index 0e647ad..9072082 100644 (file)
@@ -26,9 +26,11 @@ import ch.qos.logback.core.read.ListAppender
 import org.junit.jupiter.api.AfterEach
 import org.junit.jupiter.api.BeforeEach
 import org.onap.cps.api.CpsAdminService
+import org.onap.cps.api.CpsDataService
 import org.onap.cps.api.CpsModuleService
 import org.onap.cps.ncmp.api.impl.exception.NcmpStartUpException
 import org.onap.cps.spi.exceptions.AlreadyDefinedException
+import org.onap.cps.spi.exceptions.DataValidationException
 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException
 import org.springframework.boot.SpringApplication
 import org.slf4j.LoggerFactory
@@ -39,11 +41,13 @@ class SubscriptionModelLoaderSpec extends Specification {
 
     def mockCpsAdminService = Mock(CpsAdminService)
     def mockCpsModuleService = Mock(CpsModuleService)
-    def objectUnderTest = new SubscriptionModelLoader(mockCpsAdminService, mockCpsModuleService)
+    def mockCpsDataService = Mock(CpsDataService)
+    def objectUnderTest = new SubscriptionModelLoader(mockCpsAdminService, mockCpsModuleService, mockCpsDataService)
 
     def SUBSCRIPTION_DATASPACE_NAME = objectUnderTest.SUBSCRIPTION_DATASPACE_NAME;
     def SUBSCRIPTION_ANCHOR_NAME = objectUnderTest.SUBSCRIPTION_ANCHOR_NAME;
     def SUBSCRIPTION_SCHEMASET_NAME = objectUnderTest.SUBSCRIPTION_SCHEMASET_NAME;
+    def SUBSCRIPTION_REGISTRY_DATANODE_NAME = objectUnderTest.SUBSCRIPTION_REGISTRY_DATANODE_NAME;
 
     def sampleYangContentMap = ['subscription.yang':'module subscription { *sample content* }']
 
@@ -75,6 +79,8 @@ class SubscriptionModelLoaderSpec extends Specification {
             1 * mockCpsModuleService.createSchemaSet(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME,sampleYangContentMap)
         and: 'the admin service to create an anchor set is called once'
             1 * mockCpsAdminService.createAnchor(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_SCHEMASET_NAME, SUBSCRIPTION_ANCHOR_NAME)
+        and: 'the data service to create a top level datanode is called once'
+            1 * mockCpsDataService.saveData(SUBSCRIPTION_DATASPACE_NAME, SUBSCRIPTION_ANCHOR_NAME, '{"' + SUBSCRIPTION_REGISTRY_DATANODE_NAME + '":{}}', _)
     }
 
     def 'Create schema set from model file'() {
@@ -138,6 +144,29 @@ class SubscriptionModelLoaderSpec extends Specification {
             thrown(NcmpStartUpException)
     }
 
+    def 'Create top level node fails due to an AlreadyDefined exception'() {
+        given: 'the saving of the node data will throw an Already Defined exception'
+            mockCpsDataService.saveData(*_) >>
+                { AlreadyDefinedException.forDataNode('/xpath', "sampleContextName", null) }
+        when: 'the method to onboard model is called'
+            objectUnderTest.onboardSubscriptionModel()
+        then: 'no exception thrown'
+            noExceptionThrown()
+    }
+
+    def 'Create top level node fails due to any other exception'() {
+        given: 'the saving of the node data will throw an exception'
+            mockCpsDataService.saveData(*_) >>
+                { throw new DataValidationException("Invalid JSON", "JSON Data is invalid") }
+        when: 'the method to onboard model is called'
+            objectUnderTest.onboardSubscriptionModel()
+        then: 'the log message contains the correct exception message'
+            def debugMessage = appender.list[0].toString()
+            assert debugMessage.contains("Creating data node for subscription model failed: Invalid JSON")
+        and: 'exception is thrown'
+            thrown(NcmpStartUpException)
+    }
+
     def 'Get file content as string'() {
         when: 'the method to get yang content is called'
             def response = objectUnderTest.getFileContentAsString()