From 723994d3a9fb5909c1035db7f520a07fb6995ee0 Mon Sep 17 00:00:00 2001 From: emaclee Date: Wed, 17 Jan 2024 11:59:29 +0000 Subject: Create datastores for NCMP w/subscription model Issue-ID: CPS-2035 Change-Id: I80234c7ca93c5774ee50082803cdc102f693a5a7 Signed-off-by: emaclee --- .../onap/cps/ncmp/init/AbstractModelLoader.java | 4 +-- .../ncmp/init/CmDataSubscriptionModelLoader.java | 32 +++++++++++++++++--- .../onap/cps/ncmp/init/InventoryModelLoader.java | 8 ++--- .../cps/ncmp/init/AbstractModelLoaderSpec.groovy | 10 +++---- .../init/CmDataSubscriptionModelLoaderSpec.groovy | 35 ++++++++++++++++++++-- .../cps/ncmp/init/InventoryModelLoaderSpec.groovy | 4 +-- 6 files changed, 74 insertions(+), 19 deletions(-) diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/AbstractModelLoader.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/AbstractModelLoader.java index bd8dec4dc8..a3ebec3c54 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/AbstractModelLoader.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/AbstractModelLoader.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation + * Copyright (C) 2023-2024 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,8 +47,8 @@ abstract class AbstractModelLoader implements ModelLoader { private final CpsDataspaceService cpsDataspaceService; private final CpsModuleService cpsModuleService; - private final CpsDataService cpsDataService; private final CpsAnchorService cpsAnchorService; + protected final CpsDataService cpsDataService; private static final int EXIT_CODE_ON_ERROR = 1; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/CmDataSubscriptionModelLoader.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/CmDataSubscriptionModelLoader.java index c0f0279ad4..81055db847 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/CmDataSubscriptionModelLoader.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/CmDataSubscriptionModelLoader.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation + * Copyright (C) 2024 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,12 +21,16 @@ package org.onap.cps.ncmp.init; import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DATASPACE_NAME; +import static org.onap.cps.utils.ContentType.JSON; +import java.time.OffsetDateTime; import lombok.extern.slf4j.Slf4j; import org.onap.cps.api.CpsAnchorService; import org.onap.cps.api.CpsDataService; import org.onap.cps.api.CpsDataspaceService; import org.onap.cps.api.CpsModuleService; +import org.onap.cps.ncmp.api.impl.exception.NcmpStartUpException; +import org.onap.cps.spi.exceptions.AlreadyDefinedException; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -38,6 +42,8 @@ public class CmDataSubscriptionModelLoader extends AbstractModelLoader { private static final String SCHEMASET_NAME = "cm-data-subscriptions"; private static final String ANCHOR_NAME = "cm-data-subscriptions"; private static final String REGISTRY_DATANODE_NAME = "datastores"; + private static final String DATASTORE_PASSTHROUGH_OPERATIONAL = "ncmp-datastores:passthrough-operational"; + private static final String DATASTORE_PASSTHROUGH_RUNNING = "ncmp-datastores:passthrough-running"; private static final String DEPRECATED_MODEL_FILENAME = "subscription.yang"; private static final String DEPRECATED_ANCHOR_NAME = "AVC-Subscriptions"; @@ -48,9 +54,9 @@ public class CmDataSubscriptionModelLoader extends AbstractModelLoader { public CmDataSubscriptionModelLoader(final CpsDataspaceService cpsDataspaceService, final CpsModuleService cpsModuleService, - final CpsDataService cpsDataService, - final CpsAnchorService cpsAnchorService) { - super(cpsDataspaceService, cpsModuleService, cpsDataService, cpsAnchorService); + final CpsAnchorService cpsAnchorService, + final CpsDataService cpsDataService) { + super(cpsDataspaceService, cpsModuleService, cpsAnchorService, cpsDataService); } @Value("${ncmp.model-loader.subscription:true}") @@ -75,5 +81,23 @@ public class CmDataSubscriptionModelLoader extends AbstractModelLoader { createSchemaSet(NCMP_DATASPACE_NAME, SCHEMASET_NAME, MODEL_FILENAME); createAnchor(NCMP_DATASPACE_NAME, SCHEMASET_NAME, ANCHOR_NAME); createTopLevelDataNode(NCMP_DATASPACE_NAME, ANCHOR_NAME, REGISTRY_DATANODE_NAME); + createDatastore(DATASTORE_PASSTHROUGH_OPERATIONAL, DATASTORE_PASSTHROUGH_RUNNING); } + + private void createDatastore(final String... datastoreNames) { + for (final String datastoreName : datastoreNames) { + final String nodeData = "{\"datastore\":[{\"name\":\"" + datastoreName + "\",\"cm-handles\":{}}]}"; + try { + cpsDataService.saveData(NCMP_DATASPACE_NAME, ANCHOR_NAME, "/" + REGISTRY_DATANODE_NAME, nodeData, + OffsetDateTime.now(), JSON); + } catch (final AlreadyDefinedException exception) { + log.info("Creating new child data node '{}' for data node '{}' failed as data node already exists", + datastoreName, REGISTRY_DATANODE_NAME); + } catch (final Exception exception) { + log.error("Creating data node failed: {}", exception.getMessage()); + throw new NcmpStartUpException("Creating data node failed", exception.getMessage()); + } + } + } + } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/InventoryModelLoader.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/InventoryModelLoader.java index 0e562cdd8b..01bfc2b5d4 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/InventoryModelLoader.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/InventoryModelLoader.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation + * Copyright (C) 2023-2024 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,9 +39,9 @@ public class InventoryModelLoader extends AbstractModelLoader { public InventoryModelLoader(final CpsDataspaceService cpsDataspaceService, final CpsModuleService cpsModuleService, - final CpsDataService cpsDataService, - final CpsAnchorService cpsAnchorService) { - super(cpsDataspaceService, cpsModuleService, cpsDataService, cpsAnchorService); + final CpsAnchorService cpsAnchorService, + final CpsDataService cpsDataService) { + super(cpsDataspaceService, cpsModuleService, cpsAnchorService, cpsDataService); } @Override diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/AbstractModelLoaderSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/AbstractModelLoaderSpec.groovy index f9ecd63376..a9f24667f8 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/AbstractModelLoaderSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/AbstractModelLoaderSpec.groovy @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation + * Copyright (C) 2023-2024 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,7 +42,7 @@ class AbstractModelLoaderSpec extends Specification { def mockCpsModuleService = Mock(CpsModuleService) def mockCpsDataService = Mock(CpsDataService) def mockCpsAnchorService = Mock(CpsAnchorService) - def objectUnderTest = Spy(new TestModelLoader(mockCpsDataspaceService, mockCpsModuleService, mockCpsDataService, mockCpsAnchorService)) + def objectUnderTest = Spy(new TestModelLoader(mockCpsDataspaceService, mockCpsModuleService, mockCpsAnchorService, mockCpsDataService)) def applicationContext = new AnnotationConfigApplicationContext() @@ -221,9 +221,9 @@ class AbstractModelLoaderSpec extends Specification { TestModelLoader(final CpsDataspaceService cpsDataspaceService, final CpsModuleService cpsModuleService, - final CpsDataService cpsDataService, - final CpsAnchorService cpsAnchorService) { - super(cpsDataspaceService, cpsModuleService, cpsDataService, cpsAnchorService) + final CpsAnchorService cpsAnchorService, + final CpsDataService cpsDataService) { + super(cpsDataspaceService, cpsModuleService, cpsAnchorService, cpsDataService) super.maximumAttemptCount = 2 super.retryTimeMs = 1 } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/CmDataSubscriptionModelLoaderSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/CmDataSubscriptionModelLoaderSpec.groovy index aed495ec45..9722fe8c67 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/CmDataSubscriptionModelLoaderSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/CmDataSubscriptionModelLoaderSpec.groovy @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation + * Copyright (C) 2023-2024 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,8 @@ package org.onap.cps.ncmp.init import org.onap.cps.api.CpsAnchorService +import org.onap.cps.ncmp.api.impl.exception.NcmpStartUpException +import org.onap.cps.spi.exceptions.AlreadyDefinedException import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DATASPACE_NAME @@ -42,7 +44,7 @@ class CmDataSubscriptionModelLoaderSpec extends Specification { def mockCpsModuleService = Mock(CpsModuleService) def mockCpsDataService = Mock(CpsDataService) def mockCpsAnchorService = Mock(CpsAnchorService) - def objectUnderTest = new CmDataSubscriptionModelLoader(mockCpsDataspaceService, mockCpsModuleService, mockCpsDataService, mockCpsAnchorService) + def objectUnderTest = new CmDataSubscriptionModelLoader(mockCpsDataspaceService, mockCpsModuleService, mockCpsAnchorService, mockCpsDataService) def applicationContext = new AnnotationConfigApplicationContext() @@ -77,6 +79,35 @@ class CmDataSubscriptionModelLoaderSpec extends Specification { 1 * mockCpsAnchorService.createAnchor(NCMP_DATASPACE_NAME, 'cm-data-subscriptions', 'cm-data-subscriptions') and: 'the data service to create a top level datanode is called once' 1 * mockCpsDataService.saveData(NCMP_DATASPACE_NAME, 'cm-data-subscriptions', '{"datastores":{}}', _) + and: 'the data service is called once to create datastore for Passthrough-operational' + 1 * mockCpsDataService.saveData(NCMP_DATASPACE_NAME, 'cm-data-subscriptions', '/datastores', + '{"datastore":[{"name":"ncmp-datastores:passthrough-operational","cm-handles":{}}]}', _, _) + and: 'the data service is called once to create datastore for Passthrough-running' + 1 * mockCpsDataService.saveData(NCMP_DATASPACE_NAME, 'cm-data-subscriptions', '/datastores', + '{"datastore":[{"name":"ncmp-datastores:passthrough-running","cm-handles":{}}]}', _, _) + } + + def 'Create node for datastore with already defined exception.'() { + given:'the data service throws an Already Defined exception' + mockCpsDataService.saveData(*_) >> { throw AlreadyDefinedException.forDataNodes([], 'some context') } + when: 'attempt to create datastore' + objectUnderTest.createDatastore('some datastore') + then: 'the exception is ignored i.e. no exception thrown up' + noExceptionThrown() + and: 'the exception message is logged' + def logs = loggingListAppender.list.toString() + logs.contains("Creating new child data node 'some datastore' for data node 'datastores' failed as data node already exists") + } + + def 'Create node for datastore with any other exception.'() { + given: 'the data service throws an exception' + mockCpsDataService.saveData(*_) >> { throw new RuntimeException('test message') } + when: 'attempt to create datastore' + objectUnderTest.createDatastore('some datastore') + then: 'a startup exception with correct message and details is thrown' + def thrown = thrown(NcmpStartUpException) + assert thrown.message.contains('Creating data node failed') + assert thrown.details.contains('test message') } def 'Subscription model loader disabled.' () { diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/InventoryModelLoaderSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/InventoryModelLoaderSpec.groovy index 5557993abd..a36532761c 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/InventoryModelLoaderSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/InventoryModelLoaderSpec.groovy @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation + * Copyright (C) 2023-2024 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,7 +43,7 @@ class InventoryModelLoaderSpec extends Specification { def mockCpsModuleService = Mock(CpsModuleService) def mockCpsDataService = Mock(CpsDataService) def mockCpsAnchorService = Mock(CpsAnchorService) - def objectUnderTest = new InventoryModelLoader(mockCpsAdminService, mockCpsModuleService, mockCpsDataService, mockCpsAnchorService) + def objectUnderTest = new InventoryModelLoader(mockCpsAdminService, mockCpsModuleService, mockCpsAnchorService, mockCpsDataService) def applicationContext = new AnnotationConfigApplicationContext() -- cgit 1.2.3-korg