Do not create the operational/configuration cache if it already exists 30/2630/1
authorMoiz Raja <moraja@cisco.com>
Mon, 11 Nov 2013 21:54:40 +0000 (13:54 -0800)
committerMoiz Raja <moraja@cisco.com>
Mon, 11 Nov 2013 21:55:27 +0000 (13:55 -0800)
Change-Id: Ib658499f8eaf81dcad91df8a26539287644a292b
Signed-off-by: Moiz Raja <moraja@cisco.com>
opendaylight/md-sal/clustered-data-store/implementation/src/main/java/org/opendaylight/controller/datastore/internal/ClusteredDataStoreImpl.java
opendaylight/md-sal/clustered-data-store/implementation/src/test/java/org/opendaylight/controller/datastore/internal/ClusteredDataStoreImplTest.java

index f2e7773a454565e537053f72889cc0ebfb0a3f6a..18b98925637cb1c2d5fc9dbfd39a8c0a72001a48 100644 (file)
@@ -35,19 +35,19 @@ public class ClusteredDataStoreImpl implements ClusteredDataStore {
     public static final String OPERATIONAL_DATA_CACHE = "clustered_data_store.operational_data_cache";
     public static final String CONFIGURATION_DATA_CACHE = "clustered_data_store.configuration_data_cache";
 
-    private ConcurrentMap operationalDataCache;
-    private ConcurrentMap configurationDataCache;
+    private final ConcurrentMap operationalDataCache;
+    private final ConcurrentMap configurationDataCache;
 
     public ClusteredDataStoreImpl(IClusterGlobalServices clusterGlobalServices) throws CacheExistException, CacheConfigException {
         Preconditions.checkNotNull(clusterGlobalServices, "clusterGlobalServices cannot be null");
 
-        operationalDataCache = clusterGlobalServices.createCache(OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
+        operationalDataCache = getOrCreateCache(clusterGlobalServices, OPERATIONAL_DATA_CACHE);
 
         if(operationalDataCache == null){
             Preconditions.checkNotNull(operationalDataCache, "operationalDataCache cannot be null");
         }
 
-        configurationDataCache = clusterGlobalServices.createCache(CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
+        configurationDataCache = getOrCreateCache(clusterGlobalServices, CONFIGURATION_DATA_CACHE);
 
         if(configurationDataCache == null){
             Preconditions.checkNotNull(configurationDataCache, "configurationDataCache cannot be null");
@@ -95,6 +95,20 @@ public class ClusteredDataStoreImpl implements ClusteredDataStore {
       return Rpcs.<Void>getRpcResult(true, null, _emptySet);
     }
 
+
+    private ConcurrentMap getOrCreateCache(IClusterGlobalServices clusterGlobalServices, String name) throws CacheConfigException {
+        ConcurrentMap cache = clusterGlobalServices.getCache(name);
+
+        if(cache == null) {
+            try {
+                cache = clusterGlobalServices.createCache(name, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
+            } catch (CacheExistException e) {
+                cache = clusterGlobalServices.getCache(name);
+            }
+        }
+        return cache;
+    }
+
     private class ClusteredDataStoreTransaction implements DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> {
         private final DataModification<InstanceIdentifier<? extends Object>,Object> modification;
 
index 8049bae5af602845c0b1b34616aef7a0cc1f6b30..d7ae4bf2e5d18797f092acb274dab972bd90bbd1 100644 (file)
@@ -64,6 +64,31 @@ public class ClusteredDataStoreImplTest {
         }
     }
 
+    @Test
+    public void constructor_WhenOperationalDataCacheIsAlreadyPresent_ShouldNotAttemptToCreateCache() throws CacheExistException, CacheConfigException {
+        IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
+
+        Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
+        Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
+
+        new ClusteredDataStoreImpl(mockClusterGlobalServices);
+
+        verify(mockClusterGlobalServices, never()).createCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
+    }
+
+    @Test
+    public void constructor_WhenConfigurationDataCacheIsAlreadyPresent_ShouldNotAttemptToCreateCache() throws CacheExistException, CacheConfigException {
+        IClusterGlobalServices mockClusterGlobalServices = mock(IClusterGlobalServices.class);
+
+        Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.OPERATIONAL_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
+        Mockito.<ConcurrentMap<?,?>>when(mockClusterGlobalServices.getCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE)).thenReturn(new ConcurrentHashMap<Object, Object>());
+
+        new ClusteredDataStoreImpl(mockClusterGlobalServices);
+
+        verify(mockClusterGlobalServices, never()).createCache(ClusteredDataStoreImpl.CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
+    }
+
+
     @Test
     public void constructor_WhenPassedAValidClusteringServices_ShouldNotThrowAnyExceptions() throws CacheExistException, CacheConfigException {
         IClusterGlobalServices mockClusterGlobalServices = createClusterGlobalServices();