Introduce and use new Hazelcast map pt. 2 06/136206/10
authorsourabh_sourabh <sourabh.sourabh@est.tech>
Mon, 23 Oct 2023 13:40:25 +0000 (14:40 +0100)
committersourabh_sourabh <sourabh.sourabh@est.tech>
Mon, 23 Oct 2023 15:19:50 +0000 (16:19 +0100)
   - modified ModuleSyncService
   - created ModuleSetTagCacheConfig and tests
   - ensure both Create and Upgrade cm Handle use cases work
   - Moved inventory pkg to its right patha as production code.

Issue-ID: CPS-1859
Signed-off-by: leventecsanyi <levente.csanyi@est.tech>
Change-Id: I173dcd81fe2e74d86d85365b2ead461302cad974
Signed-off-by: sourabh_sourabh <sourabh.sourabh@est.tech>
21 files changed:
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/ModuleSetTagCacheConfig.java [new file with mode: 0644]
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfig.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncService.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncWatchdog.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/yangmodels/YangModelCmHandle.java
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/embeddedcache/ModuleSetTagCacheConfigSpec.groovy [new file with mode: 0644]
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/embeddedcache/SynchronizationCacheConfigSpec.groovy
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/CmHandleQueriesImplSpec.groovy [moved from cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/CmHandleQueriesImplSpec.groovy with 99% similarity]
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/CompositeStateBuilderSpec.groovy [moved from cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/CompositeStateBuilderSpec.groovy with 99% similarity]
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/CompositeStateSpec.groovy [moved from cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/CompositeStateSpec.groovy with 98% similarity]
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/InventoryPersistenceImplSpec.groovy [moved from cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/InventoryPersistenceImplSpec.groovy with 99% similarity]
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/DataSyncWatchdogSpec.groovy [moved from cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/DataSyncWatchdogSpec.groovy with 99% similarity]
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncServiceSpec.groovy [moved from cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncServiceSpec.groovy with 63% similarity]
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncTasksSpec.groovy [moved from cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncTasksSpec.groovy with 99% similarity]
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncWatchdogSpec.groovy [moved from cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncWatchdogSpec.groovy with 97% similarity]
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/SyncUtilsSpec.groovy [moved from cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/SyncUtilsSpec.groovy with 99% similarity]
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/config/WatchdogSchedulingConfigurerSpec.groovy [moved from cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/config/WatchdogSchedulingConfigurerSpec.groovy with 97% similarity]
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/executor/AsyncTaskExecutorSpec.groovy [moved from cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/executor/AsyncTaskExecutorSpec.groovy with 97% similarity]
cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java

index e7ffaa6..0c12875 100644 (file)
@@ -171,7 +171,7 @@ public interface NetworkCmProxyDataService {
      * @param cmHandleId cm handle id
      * @param dataSyncEnabled data sync enabled flag
      */
-    void setDataSyncEnabled(String cmHandleId, boolean dataSyncEnabled);
+    void setDataSyncEnabled(String cmHandleId, Boolean dataSyncEnabled);
 
     /**
      * Get all cm handle IDs by DMI plugin identifier.
index b2c7194..febd1cf 100755 (executable)
@@ -218,26 +218,29 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService
      * based on the data sync enabled boolean for the cm handle id provided.
      *
      * @param cmHandleId      cm handle id
-     * @param dataSyncEnabled data sync enabled flag
+     * @param dataSyncEnabledTargetValue data sync enabled flag
      */
     @Override
-    public void setDataSyncEnabled(final String cmHandleId, final boolean dataSyncEnabled) {
+    public void setDataSyncEnabled(final String cmHandleId, final Boolean dataSyncEnabledTargetValue) {
         final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
-        if (compositeState.getDataSyncEnabled().equals(dataSyncEnabled)) {
-            log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabled);
-        } else if (compositeState.getCmHandleState() != CmHandleState.READY) {
-            throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
-                    + compositeState.getCmHandleState());
-        } else {
+        if (dataSyncEnabledTargetValue.equals(compositeState.getDataSyncEnabled())) {
+            log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabledTargetValue);
+            return;
+        }
+        if (CmHandleState.READY.equals(compositeState.getCmHandleState())) {
             final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
                     .getOperationalDataStore().getDataStoreSyncState();
-            if (!dataSyncEnabled && dataStoreSyncState == DataStoreSyncState.SYNCHRONIZED) {
+            if (Boolean.FALSE.equals(dataSyncEnabledTargetValue)
+                    && DataStoreSyncState.SYNCHRONIZED.equals(dataStoreSyncState)) {
+                // TODO : This is hard-coded for onap dmi that need to be addressed
                 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
                         "/netconf-state", OffsetDateTime.now());
             }
-            CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabled, compositeState);
-            inventoryPersistence.saveCmHandleState(cmHandleId,
-                    compositeState);
+            CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabledTargetValue, compositeState);
+            inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
+        } else {
+            throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
+                    + compositeState.getCmHandleState());
         }
     }
 
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/ModuleSetTagCacheConfig.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/embeddedcache/ModuleSetTagCacheConfig.java
new file mode 100644 (file)
index 0000000..a791a3b
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2023 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  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.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.ncmp.api.impl.config.embeddedcache;
+
+import com.hazelcast.config.MapConfig;
+import java.util.Collection;
+import java.util.Map;
+import org.onap.cps.cache.HazelcastCacheConfig;
+import org.onap.cps.spi.model.ModuleReference;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class ModuleSetTagCacheConfig extends HazelcastCacheConfig {
+
+    private static final MapConfig moduleSetTagCacheMapConfig = createMapConfig("moduleSetTagCacheMapConfig");
+
+    /**
+     * Map instance for cached ModulesSetTags.
+     *
+     * @return configured map of ModuleSetTags
+     */
+    @Bean
+    public Map<String, Collection<ModuleReference>> moduleSetTagCache() {
+        return createHazelcastInstance("moduleSetTags", moduleSetTagCacheMapConfig)
+                .getMap("moduleSetTagCache");
+    }
+}
index 8b28717..62a380c 100644 (file)
@@ -23,7 +23,6 @@ package org.onap.cps.ncmp.api.impl.config.embeddedcache;
 import com.hazelcast.config.MapConfig;
 import com.hazelcast.config.QueueConfig;
 import com.hazelcast.map.IMap;
-import java.util.Set;
 import java.util.concurrent.BlockingQueue;
 import lombok.extern.slf4j.Slf4j;
 import org.onap.cps.cache.HazelcastCacheConfig;
@@ -45,8 +44,6 @@ public class SynchronizationCacheConfig extends HazelcastCacheConfig {
     private static final MapConfig moduleSyncStartedConfig = createMapConfig("moduleSyncStartedConfig");
     private static final MapConfig dataSyncSemaphoresConfig = createMapConfig("dataSyncSemaphoresConfig");
 
-    private static final MapConfig moduleSetTagCacheMapConfig = createMapConfig("moduleSetTagCacheMapConfig");
-
     /**
      * Module Sync Distributed Queue Instance.
      *
@@ -77,15 +74,4 @@ public class SynchronizationCacheConfig extends HazelcastCacheConfig {
     public IMap<String, Boolean> dataSyncSemaphores() {
         return createHazelcastInstance("dataSyncSemaphores", dataSyncSemaphoresConfig).getMap("dataSyncSemaphores");
     }
-
-    /**
-     * IMap instance for cached ModulesSetTags.
-     *
-     * @return configured map of ModuleSetTags
-     */
-    @Bean
-    public IMap<String, Set<String>> moduleSetTagCache() {
-        return createHazelcastInstance("moduleSetTags", moduleSetTagCacheMapConfig)
-                .getMap("moduleSetTagCache");
-    }
 }
index 8e17ab9..2ea39e8 100644 (file)
@@ -26,7 +26,6 @@ import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DM
 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
 
 import java.time.OffsetDateTime;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -65,51 +64,65 @@ public class ModuleSyncService {
     private final CmHandleQueries cmHandleQueries;
     private final CpsDataService cpsDataService;
     private final JsonObjectMapper jsonObjectMapper;
+    private final Map<String, Collection<ModuleReference>> moduleSetTagCache;
+    private static final Map<String, String> NO_NEW_MODULES = Collections.emptyMap();
 
     /**
      * This method registers a cm handle and initiates modules sync.
      *
-     * @param upgradedCmHandle the yang model of cm handle.
+     * @param yangModelCmHandle the yang model of cm handle.
      */
-    public void syncAndCreateOrUpgradeSchemaSetAndAnchor(final YangModelCmHandle upgradedCmHandle) {
+    public void syncAndCreateOrUpgradeSchemaSetAndAnchor(final YangModelCmHandle yangModelCmHandle) {
 
-        final String moduleSetTag = extractModuleSetTag(upgradedCmHandle.getCompositeState());
-        final Optional<DataNode> existingCmHandleWithSameModuleSetTag
-                = getFirstReadyDataNodeWithModuleSetTag(moduleSetTag);
+        final String moduleSetTag;
+        final String cmHandleId = yangModelCmHandle.getId();
+        final CompositeState compositeState = yangModelCmHandle.getCompositeState();
+        final boolean inUpgrade = isInUpgrade(compositeState);
 
-        if (existingCmHandleWithSameModuleSetTag.isPresent()) {
-            upgradeUsingModuleSetTag(upgradedCmHandle, moduleSetTag);
+        if (inUpgrade) {
+            moduleSetTag = extractModuleSetTag(compositeState);
         } else {
-            syncAndCreateSchemaSetAndAnchor(upgradedCmHandle);
+            moduleSetTag = yangModelCmHandle.getModuleSetTag();
         }
-        setCmHandleModuleSetTag(upgradedCmHandle, moduleSetTag);
+
+        final Collection<ModuleReference> moduleReferencesFromCache = moduleSetTagCache.get(moduleSetTag);
+
+        if (moduleReferencesFromCache == null) {
+            final Optional<DataNode> optionalExistingCmHandleWithSameModuleSetTag
+                    = getFirstReadyDataNodeWithModuleSetTag(moduleSetTag);
+
+            if (optionalExistingCmHandleWithSameModuleSetTag.isPresent()) {
+                final String existingCmHandleAnchorName
+                        = optionalExistingCmHandleWithSameModuleSetTag.get().getAnchorName();
+                createOrUpgradeSchemaSetUsingModuleSetTag(cmHandleId, moduleSetTag, existingCmHandleAnchorName);
+            } else {
+                syncAndCreateSchemaSet(yangModelCmHandle, moduleSetTag);
+            }
+        } else {
+            cpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
+                    cmHandleId, NO_NEW_MODULES, moduleReferencesFromCache);
+        }
+        if (!inUpgrade) {
+            cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, cmHandleId);
+        }
+        setCmHandleModuleSetTag(yangModelCmHandle, moduleSetTag);
     }
 
-    private void syncAndCreateSchemaSetAndAnchor(final YangModelCmHandle yangModelCmHandle) {
+    private void syncAndCreateSchemaSet(final YangModelCmHandle yangModelCmHandle, final String moduleSetTag) {
         final Collection<ModuleReference> allModuleReferencesFromCmHandle =
                 dmiModelOperations.getModuleReferences(yangModelCmHandle);
-
         final Collection<ModuleReference> identifiedNewModuleReferencesFromCmHandle = cpsModuleService
                 .identifyNewModuleReferences(allModuleReferencesFromCmHandle);
-
         final Map<String, String> newModuleNameToContentMap;
         if (identifiedNewModuleReferencesFromCmHandle.isEmpty()) {
-            newModuleNameToContentMap = Collections.emptyMap();
+            newModuleNameToContentMap = NO_NEW_MODULES;
         } else {
             newModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle,
                     identifiedNewModuleReferencesFromCmHandle);
         }
-        createSchemaSetAndAnchor(yangModelCmHandle, newModuleNameToContentMap, allModuleReferencesFromCmHandle);
-    }
-
-    private void createSchemaSetAndAnchor(final YangModelCmHandle yangModelCmHandle,
-                                          final Map<String, String> newModuleNameToContentMap,
-                                          final Collection<ModuleReference> allModuleReferencesFromCmHandle) {
-        final String schemaSetAndAnchorName = yangModelCmHandle.getId();
         cpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
-                schemaSetAndAnchorName, newModuleNameToContentMap, allModuleReferencesFromCmHandle);
-        cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetAndAnchorName,
-            schemaSetAndAnchorName);
+            yangModelCmHandle.getId(), newModuleNameToContentMap, allModuleReferencesFromCmHandle);
+        moduleSetTagCache.put(moduleSetTag, allModuleReferencesFromCmHandle);
     }
 
     /**
@@ -147,21 +160,24 @@ public class ModuleSyncService {
                 jsonObjectMapper.asJsonString(dmiRegistryProperties), OffsetDateTime.now());
     }
 
-    private void upgradeUsingModuleSetTag(final YangModelCmHandle upgradedCmHandle, final String moduleSetTag) {
+    private void createOrUpgradeSchemaSetUsingModuleSetTag(final String schemaSetName,
+                                                           final String moduleSetTag,
+                                                           final String existingCmHandleAnchorName) {
         log.info("Found cm handle having module set tag: {}", moduleSetTag);
         final Collection<ModuleReference> moduleReferencesFromExistingCmHandle =
-                cpsModuleService.getYangResourcesModuleReferences(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR);
-        final String upgradedSchemaSetAndAnchorName = upgradedCmHandle.getId();
-        final Map<String, String> noNewModules = Collections.emptyMap();
+                cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
+                        existingCmHandleAnchorName);
         cpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
-                upgradedSchemaSetAndAnchorName, noNewModules, moduleReferencesFromExistingCmHandle);
+            schemaSetName, NO_NEW_MODULES, moduleReferencesFromExistingCmHandle);
+        moduleSetTagCache.put(moduleSetTag, moduleReferencesFromExistingCmHandle);
     }
 
     private static String extractModuleSetTag(final CompositeState compositeState) {
-        return compositeState.getLockReason() != null && compositeState.getLockReason().getLockReasonCategory()
-                == LockReasonCategory.MODULE_UPGRADE
-                ? Arrays.stream(compositeState.getLockReason().getDetails().split(":")).toList().get(1).trim()
-                : StringUtils.EMPTY;
+        return compositeState.getLockReason().getDetails().split(":")[1].trim();
     }
 
+    private static boolean isInUpgrade(final CompositeState compositeState) {
+        return compositeState.getLockReason() != null && LockReasonCategory.MODULE_UPGRADE.equals(
+                compositeState.getLockReason().getLockReasonCategory());
+    }
 }
index 75781eb..bf00505 100644 (file)
@@ -25,7 +25,6 @@ import com.hazelcast.map.IMap;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Set;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -49,7 +48,6 @@ public class ModuleSyncWatchdog {
     private final IMap<String, Object> moduleSyncStartedOnCmHandles;
     private final ModuleSyncTasks moduleSyncTasks;
     private final AsyncTaskExecutor asyncTaskExecutor;
-    private final IMap<String, Set<String>> moduleSetTagCache;
     private static final int MODULE_SYNC_BATCH_SIZE = 100;
     private static final long PREVENT_CPU_BURN_WAIT_TIME_MILLIS = 10;
     private static final String VALUE_FOR_HAZELCAST_IN_PROGRESS_MAP = "Started";
index d148f37..f930d5b 100644 (file)
@@ -34,6 +34,7 @@ import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
 import lombok.Setter;
+import org.apache.commons.lang3.StringUtils;
 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
 import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService;
 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
@@ -112,7 +113,7 @@ public class YangModelCmHandle {
         yangModelCmHandle.setDmiServiceName(dmiServiceName);
         yangModelCmHandle.setDmiDataServiceName(dmiDataServiceName);
         yangModelCmHandle.setDmiModelServiceName(dmiModelServiceName);
-        yangModelCmHandle.setModuleSetTag(moduleSetTag);
+        yangModelCmHandle.setModuleSetTag(moduleSetTag == null ? StringUtils.EMPTY : moduleSetTag);
         yangModelCmHandle.setDmiProperties(asYangModelCmHandleProperties(ncmpServiceCmHandle.getDmiProperties()));
         yangModelCmHandle.setPublicProperties(asYangModelCmHandleProperties(
                 ncmpServiceCmHandle.getPublicProperties()));
diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/embeddedcache/ModuleSetTagCacheConfigSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/config/embeddedcache/ModuleSetTagCacheConfigSpec.groovy
new file mode 100644 (file)
index 0000000..8a6a32b
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2023 Nordix Foundation
+ *  ================================================================================
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  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.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.ncmp.api.impl.config.embeddedcache
+
+import com.hazelcast.config.Config
+import com.hazelcast.core.Hazelcast
+import org.onap.cps.spi.model.ModuleReference
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.boot.test.context.SpringBootTest
+import spock.lang.Specification
+
+@SpringBootTest(classes = [ModuleSetTagCacheConfig])
+class ModuleSetTagCacheConfigSpec extends Specification {
+
+    @Autowired
+    private Map<String, Collection<ModuleReference>> moduleSetTagCache
+
+    def 'Embedded (hazelcast) caches for module set tag.'() {
+        expect: 'system is able to create an instance of a map to hold module set tags'
+            assert null != moduleSetTagCache
+        and: 'there is at least 1 instance'
+            assert Hazelcast.allHazelcastInstances.size() > 0
+        and: 'hazelcast instance name of module set tag is correct'
+            assert Hazelcast.allHazelcastInstances.name.contains('moduleSetTags')
+    }
+
+    def 'Verify configs of module set tag distributed object.'(){
+        when: 'retrieving the map config of module set tag'
+            def cacheConfig =  Hazelcast.getHazelcastInstanceByName('moduleSetTags').config
+            def moduleSetTagMapConfig = cacheConfig.mapConfigs.get('moduleSetTagCacheMapConfig')
+        then: 'the module set tag map config has correct backup counts'
+            assert moduleSetTagMapConfig.backupCount == 3
+            assert moduleSetTagMapConfig.asyncBackupCount == 3
+    }
+
+    def 'Verify deployment network configs of distributed cache of module set tag object.'() {
+        given: 'network config of module set tag cache'
+            def moduleSetTagNetworkConfig = Hazelcast.getHazelcastInstanceByName('moduleSetTags').config.networkConfig
+        expect: 'module set tag cache has the correct settings'
+            assert moduleSetTagNetworkConfig.join.autoDetectionConfig.enabled
+            assert !moduleSetTagNetworkConfig.join.kubernetesConfig.enabled
+    }
+
+    def 'Verify network of module set tag cache'() {
+        given: 'Synchronization config object and test configuration'
+        def objectUnderTest = new ModuleSetTagCacheConfig()
+        def testConfig = new Config()
+        when: 'kubernetes properties are enabled'
+            objectUnderTest.cacheKubernetesEnabled = true
+            objectUnderTest.cacheKubernetesServiceName = 'test-service-name'
+        and: 'method called to update the discovery mode'
+            objectUnderTest.updateDiscoveryMode(testConfig)
+        then: 'applied properties are reflected'
+            assert testConfig.networkConfig.join.kubernetesConfig.enabled
+            assert testConfig.networkConfig.join.kubernetesConfig.properties.get('service-name') == 'test-service-name'
+
+    }
+}
index 2fa9606..501714a 100644 (file)
@@ -44,9 +44,6 @@ class SynchronizationCacheConfigSpec extends Specification {
     @Autowired
     private IMap<String, Boolean> dataSyncSemaphores
 
-    @Autowired
-    private IMap<String, Set<String>> moduleSetTagCache
-
     def 'Embedded (hazelcast) Caches for Module and Data Sync.'() {
         expect: 'system is able to create an instance of the Module Sync Work Queue'
             assert null != moduleSyncWorkQueue
@@ -54,12 +51,10 @@ class SynchronizationCacheConfigSpec extends Specification {
             assert null != moduleSyncStartedOnCmHandles
         and: 'system is able to create an instance of a map to hold data sync semaphores'
             assert null != dataSyncSemaphores
-        and: 'system is able to create an instance of a map to hold module set tags'
-            assert null != moduleSetTagCache
-            and: 'there are at least 4 instances'
-        assert Hazelcast.allHazelcastInstances.size() > 3
+        and: 'there are at least 3 instances'
+            assert Hazelcast.allHazelcastInstances.size() > 2
         and: 'they have the correct names (in any order)'
-            assert Hazelcast.allHazelcastInstances.name.containsAll('moduleSyncWorkQueue', 'moduleSyncStartedOnCmHandles', 'dataSyncSemaphores', 'moduleSetTags')
+            assert Hazelcast.allHazelcastInstances.name.containsAll('moduleSyncWorkQueue', 'moduleSyncStartedOnCmHandles', 'dataSyncSemaphores')
     }
 
     def 'Verify configs for Distributed objects'(){
@@ -72,9 +67,6 @@ class SynchronizationCacheConfigSpec extends Specification {
         and: 'the Data Sync Semaphores Map config'
             def dataSyncSemaphoresConfig =  Hazelcast.getHazelcastInstanceByName('dataSyncSemaphores').config
             def dataSyncSemaphoresMapConfig =  dataSyncSemaphoresConfig.mapConfigs.get('dataSyncSemaphoresConfig')
-        and: 'the Module Set Tag Map config'
-            def moduleSetTagCacheConfig =  Hazelcast.getHazelcastInstanceByName('moduleSetTags').config
-            def moduleSetTagMapConfig =  moduleSetTagCacheConfig.mapConfigs.get('moduleSetTagCacheMapConfig')
         expect: 'system created instance with correct config of Module Sync Work Queue'
             assert moduleSyncDefaultWorkQueueConfig.backupCount == 3
             assert moduleSyncDefaultWorkQueueConfig.asyncBackupCount == 3
@@ -84,15 +76,11 @@ class SynchronizationCacheConfigSpec extends Specification {
         and: 'Data Sync Semaphore Map has the correct settings'
             assert dataSyncSemaphoresMapConfig.backupCount == 3
             assert dataSyncSemaphoresMapConfig.asyncBackupCount == 3
-        and: 'Module Set Tag Map has the correct settings'
-            assert moduleSetTagMapConfig.backupCount == 3
-            assert moduleSetTagMapConfig.asyncBackupCount == 3
         and: 'all instances are part of same cluster'
             def testClusterName = 'cps-and-ncmp-test-caches'
             assert moduleSyncWorkQueueConfig.clusterName == testClusterName
             assert moduleSyncStartedOnCmHandlesConfig.clusterName == testClusterName
             assert dataSyncSemaphoresConfig.clusterName == testClusterName
-            assert moduleSetTagCacheConfig.clusterName == testClusterName
     }
 
     def 'Verify deployment network configs for Distributed objects'() {
@@ -102,8 +90,6 @@ class SynchronizationCacheConfigSpec extends Specification {
             def moduleSyncStartedOnCmHandlesNetworkConfig = Hazelcast.getHazelcastInstanceByName('moduleSyncStartedOnCmHandles').config.networkConfig
         and: 'the Data Sync Semaphores Map config'
             def dataSyncSemaphoresNetworkConfig = Hazelcast.getHazelcastInstanceByName('dataSyncSemaphores').config.networkConfig
-        and: 'the Module Set Tag Map config'
-            def moduleSetTagNetworkConfig = Hazelcast.getHazelcastInstanceByName('moduleSetTags').config.networkConfig
         expect: 'system created instance with correct config of Module Sync Work Queue'
             assert queueNetworkConfig.join.autoDetectionConfig.enabled
             assert !queueNetworkConfig.join.kubernetesConfig.enabled
@@ -113,9 +99,6 @@ class SynchronizationCacheConfigSpec extends Specification {
         and: 'Data Sync Semaphore Map has the correct settings'
             assert dataSyncSemaphoresNetworkConfig.join.autoDetectionConfig.enabled
             assert !dataSyncSemaphoresNetworkConfig.join.kubernetesConfig.enabled
-        and: 'Module Set Tag Map has the correct settings'
-            assert moduleSetTagNetworkConfig.join.autoDetectionConfig.enabled
-            assert !moduleSetTagNetworkConfig.join.kubernetesConfig.enabled
     }
 
     def 'Verify network config'() {
@@ -151,15 +134,6 @@ class SynchronizationCacheConfigSpec extends Specification {
             waitMax2SecondsForKeyExpiration(dataSyncSemaphores, 'testKeyDataSync')
     }
 
-    def 'Time to Live Verify for Module Set Tag'() {
-        when: 'the key is inserted with a TTL of 1 second'
-            moduleSetTagCache.put('testKeyModuleSetTag', ['module-set-tag'] as Set, 1, TimeUnit.SECONDS)
-        then: 'the entry is present in the map'
-            assert moduleSetTagCache.get('testKeyModuleSetTag') != null
-        and: 'the entry expires in less then 2 seconds'
-            waitMax2SecondsForKeyExpiration(moduleSetTagCache, 'testKeyModuleSetTag')
-    }
-
     def waitMax2SecondsForKeyExpiration(map, key) {
         def count = 0
         while ( map.get(key)!=null && ++count <= 20 ) {
@@ -19,7 +19,7 @@
  * ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.api.inventory
+package org.onap.cps.ncmp.api.impl.inventory
 
 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
 import org.onap.cps.ncmp.api.impl.inventory.CompositeState
@@ -18,7 +18,7 @@
  * ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.api.inventory
+package org.onap.cps.ncmp.api.impl.inventory
 
 import com.fasterxml.jackson.databind.ObjectMapper
 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
@@ -20,7 +20,7 @@
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.api.inventory
+package org.onap.cps.ncmp.api.impl.inventory
 
 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME
 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DATASPACE_NAME
@@ -18,7 +18,7 @@
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.api.inventory.sync
+package org.onap.cps.ncmp.api.impl.inventory.sync
 
 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME
 
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.api.inventory.sync
+package org.onap.cps.ncmp.api.impl.inventory.sync
 
 import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.LOCKED_MISBEHAVING
-import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DATASPACE_NAME
-import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_ANCHOR
 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME
 import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.MODULE_UPGRADE
 
@@ -32,6 +30,7 @@ import org.onap.cps.spi.model.DataNode
 import org.onap.cps.api.CpsAdminService
 import org.onap.cps.api.CpsDataService
 import org.onap.cps.api.CpsModuleService
+import org.onap.cps.spi.model.DataNodeBuilder
 import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleSyncService
 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations
 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
@@ -52,38 +51,76 @@ class ModuleSyncServiceSpec extends Specification {
     def mockCmHandleQueries = Mock(CmHandleQueries)
     def mockCpsDataService = Mock(CpsDataService)
     def mockJsonObjectMapper = Mock(JsonObjectMapper)
+    def mockModuleSetTagCache = [:]
 
     def objectUnderTest = new ModuleSyncService(mockDmiModelOperations, mockCpsModuleService, mockCpsAdminService,
-            mockCmHandleQueries, mockCpsDataService, mockJsonObjectMapper)
+            mockCmHandleQueries, mockCpsDataService, mockJsonObjectMapper, mockModuleSetTagCache)
 
     def expectedDataspaceName = NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME
+    def static cmHandleWithModuleSetTag = new DataNodeBuilder().withXpath("//cm-handles[@module-set-tag='tag-1'][@id='otherId']").withAnchor('otherId').build()
 
-    def 'Sync model for a (new) cm handle with #scenario'() {
-        given: 'a cm handle'
+    def 'Sync model for a NEW cm handle without cache with #scenario'() {
+        given: 'a cm handle having some state'
             def ncmpServiceCmHandle = new NcmpServiceCmHandle()
-            ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder().build())
-            def dmiServiceName = 'some service name'
-            ncmpServiceCmHandle.cmHandleId = 'cmHandleId-1'
-            def yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(dmiServiceName, '', '', ncmpServiceCmHandle,'')
+            ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.ADVISED).build())
+            ncmpServiceCmHandle.cmHandleId = 'ch-1'
+            def yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle('some service name', '', '', ncmpServiceCmHandle, moduleSetTag)
         and: 'DMI operations returns some module references'
             def moduleReferences =  [ new ModuleReference('module1','1'), new ModuleReference('module2','2') ]
             mockDmiModelOperations.getModuleReferences(yangModelCmHandle) >> moduleReferences
-        and: 'CPS-Core returns list of existing module resources'
-            mockCpsModuleService.getYangResourceModuleReferences(expectedDataspaceName) >> toModuleReference(existingModuleResourcesInCps)
         and: 'DMI-Plugin returns resource(s) for "new" module(s)'
             mockDmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('module1', '1')]) >> newModuleNameContentToMap
+        and: 'the module service identifies #identifiedNewModuleReferences.size() new modules'
+            mockCpsModuleService.identifyNewModuleReferences(moduleReferences) >> toModuleReferences(identifiedNewModuleReferences)
+        and: 'system contains #existingCmHandlesWithSameTag.size() cm handles with same tag'
+            mockCmHandleQueries.queryNcmpRegistryByCpsPath("//cm-handles[@module-set-tag='new-tag-1']",
+                    FetchDescendantsOption.OMIT_DESCENDANTS) >> []
         when: 'module sync is triggered'
-            mockCpsModuleService.identifyNewModuleReferences(moduleReferences) >> toModuleReference(identifiedNewModuleReferences)
             objectUnderTest.syncAndCreateOrUpgradeSchemaSetAndAnchor(yangModelCmHandle)
         then: 'create schema set from module is invoked with correct parameters'
-            1 * mockCpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'cmHandleId-1', newModuleNameContentToMap, moduleReferences)
+            1 * mockCpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'ch-1', newModuleNameContentToMap, moduleReferences)
         and: 'anchor is created with the correct parameters'
-            1 * mockCpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'cmHandleId-1', 'cmHandleId-1')
+            1 * mockCpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'ch-1', 'ch-1')
         where: 'the following parameters are used'
-            scenario             | existingModuleResourcesInCps           | identifiedNewModuleReferences | newModuleNameContentToMap
-            'one new module'     | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']]           | [module1: 'some yang source']
-            'no add. properties' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']]           | [module1: 'some yang source']
-            'no new module'      | [['module1' : '1'], ['module2' : '2']] | []                            | [:]
+            scenario         | existingModuleResourcesInCps         | identifiedNewModuleReferences | newModuleNameContentToMap     | moduleSetTag
+            'one new module' | [['module2': '2'], ['module3': '3']] | [['module1': '1']]            | [module1: 'some yang source'] | ''
+            'no new module'  | [['module1': '1'], ['module2': '2']] | []                            | [:]                           | 'new-tag-1'
+    }
+
+    def 'Upgrade model for an existing cm handle with Module Set Tag where the modules are #scenario'() {
+        given: 'a cm handle being upgraded to module set tag: tag-1'
+            def ncmpServiceCmHandle = new NcmpServiceCmHandle()
+            ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder().withLockReason(MODULE_UPGRADE, 'new moduleSetTag: tag-1').build())
+            def dmiServiceName = 'some service name'
+            ncmpServiceCmHandle.cmHandleId = 'upgraded-ch'
+            def yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(dmiServiceName, '', '', ncmpServiceCmHandle,'tag-1')
+        and: 'some module references'
+            def moduleReferences =  [ new ModuleReference('module1','1') ]
+        and: 'cache or DMI operations returns some module references for upgraded cm handle'
+            if (populateCache) {
+                mockModuleSetTagCache.put('tag-1',moduleReferences)
+            } else {
+                mockDmiModelOperations.getModuleReferences(yangModelCmHandle) >> moduleReferences
+            }
+        and: 'none of these module references are new (unknown to the system)'
+            mockCpsModuleService.identifyNewModuleReferences(moduleReferences) >> []
+        and: 'CPS-Core returns list of existing module resources for TBD'
+            mockCpsModuleService.getYangResourcesModuleReferences(*_) >> [ new ModuleReference('module1','1') ]
+        and: 'system contains #existingCmHandlesWithSameTag.size() cm handles with same tag'
+            mockCmHandleQueries.queryNcmpRegistryByCpsPath("//cm-handles[@module-set-tag='tag-1']", FetchDescendantsOption.OMIT_DESCENDANTS) >> existingCmHandlesWithSameTag
+        and: 'the other cm handle is a state ready'
+            mockCmHandleQueries.cmHandleHasState('otherId', CmHandleState.READY) >> true
+        when: 'module sync is triggered'
+            objectUnderTest.syncAndCreateOrUpgradeSchemaSetAndAnchor(yangModelCmHandle)
+        then: 'create schema set from module is invoked for the upgraded cm handle'
+            1 * mockCpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'upgraded-ch', [:], moduleReferences)
+        and: 'No anchor is created for the upgraded cm handle'
+            0 * mockCpsAdminService.createAnchor(*_)
+        where: 'the following parameters are used'
+            scenario      | populateCache | existingCmHandlesWithSameTag
+            'new'         | false         | []
+            'in cache'    | true          | []
+            'in database' | false         | [cmHandleWithModuleSetTag]
     }
 
     def 'upgrade model for a existing cm handle'() {
@@ -96,7 +133,7 @@ class ModuleSyncServiceSpec extends Specification {
             mockCmHandleQueries.cmHandleHasState('cmHandleId-1', CmHandleState.READY) >> true
         and: 'the module service returns some module references'
             def moduleReferences = [new ModuleReference('module1', '1'), new ModuleReference('module2', '2')]
-            mockCpsModuleService.getYangResourcesModuleReferences(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR) >> moduleReferences
+            mockCpsModuleService.getYangResourcesModuleReferences(*_)>> moduleReferences
         and: 'a cm handle with the same moduleSetTag can be found in the registry'
             mockCmHandleQueries.queryNcmpRegistryByCpsPath("//cm-handles[@module-set-tag='targetModuleSetTag']",
                 FetchDescendantsOption.OMIT_DESCENDANTS) >> [new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'cmHandleId-1\']', leaves: ['id': 'cmHandleId-1', 'cm-handle-state': 'READY'])]
@@ -133,17 +170,12 @@ class ModuleSyncServiceSpec extends Specification {
             result == unsupportedOperationException
     }
 
-    def 'Extract module set tag with #scenario'() {
+    def 'Extract module set tag'() {
         expect: 'the module set tag is extracted correctly'
-            assert expectedModuleSetTag == objectUnderTest.extractModuleSetTag(new CompositeStateBuilder().withLockReason(lockReason, 'new moduleSetTag: targetModuleSetTag').build())
-        where: 'the following parameters are used'
-            scenario                    | lockReason         || expectedModuleSetTag
-            'locked reason is null'     | null               || ''
-            'locked for other reason'   | LOCKED_MISBEHAVING || ''
-            'locked for module upgrade' | MODULE_UPGRADE     || 'targetModuleSetTag'
+            assert 'targetModuleSetTag' == objectUnderTest.extractModuleSetTag(new CompositeStateBuilder().withLockReason(MODULE_UPGRADE, 'new moduleSetTag: targetModuleSetTag').build())
     }
 
-    def toModuleReference(moduleReferenceAsMap) {
+    def toModuleReferences(moduleReferenceAsMap) {
         def moduleReferences = [].withDefault { [:] }
         moduleReferenceAsMap.forEach(property ->
             property.forEach((moduleName, revision) -> {
@@ -19,7 +19,7 @@
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.api.inventory.sync
+package org.onap.cps.ncmp.api.impl.inventory.sync
 
 import com.hazelcast.map.IMap
 import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleSyncTasks
@@ -45,9 +45,7 @@ class ModuleSyncWatchdogSpec extends Specification {
 
     def spiedAsyncTaskExecutor = Spy(AsyncTaskExecutor)
 
-    def moduleSetTagCache = Mock(IMap<String, Set<String>>)
-
-    def objectUnderTest = new ModuleSyncWatchdog(mockSyncUtils, moduleSyncWorkQueue , mockModuleSyncStartedOnCmHandles, mockModuleSyncTasks, spiedAsyncTaskExecutor, moduleSetTagCache)
+    def objectUnderTest = new ModuleSyncWatchdog(mockSyncUtils, moduleSyncWorkQueue , mockModuleSyncStartedOnCmHandles, mockModuleSyncTasks, spiedAsyncTaskExecutor)
 
     void setup() {
         spiedAsyncTaskExecutor.setupThreadPool()
@@ -19,7 +19,7 @@
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.api.inventory.sync
+package org.onap.cps.ncmp.api.impl.inventory.sync
 
 import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.LOCKED_MISBEHAVING
 import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_OPERATIONAL
@@ -18,7 +18,7 @@
  *  ============LICENSE_END=========================================================
  */
 
-package org.onap.cps.ncmp.api.inventory.sync.executor
+package org.onap.cps.ncmp.api.impl.inventory.sync.executor
 
 import org.onap.cps.ncmp.api.impl.inventory.sync.executor.AsyncTaskExecutor
 import org.springframework.beans.factory.annotation.Autowired
index 444c895..d274b51 100644 (file)
@@ -72,7 +72,6 @@ public class CpsModuleServiceImpl implements CpsModuleService {
         cpsValidator.validateNameCharacters(dataspaceName, schemaSetName);
         cpsModulePersistenceService.storeSchemaSetFromModules(dataspaceName, schemaSetName,
             newModuleNameToContentMap, allModuleReferences);
-
     }
 
     @Override