Merge "Change target to ${project.build.target} in a bunch of pom file. Add some...
[controller.git] / opendaylight / md-sal / clustered-data-store / implementation / src / main / java / org / opendaylight / controller / datastore / internal / ClusteredDataStoreImpl.java
index f2e7773a454565e537053f72889cc0ebfb0a3f6a..0809ba347ba1bf97d914208978e9c536a3e669cf 100644 (file)
@@ -10,6 +10,7 @@
 package org.opendaylight.controller.datastore.internal;
 
 import com.google.common.base.Preconditions;
+
 import org.opendaylight.controller.clustering.services.CacheConfigException;
 import org.opendaylight.controller.clustering.services.CacheExistException;
 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
@@ -17,9 +18,12 @@ import org.opendaylight.controller.clustering.services.IClusterServices;
 import org.opendaylight.controller.datastore.ClusteredDataStore;
 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
 import org.opendaylight.controller.sal.common.util.Rpcs;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.data.api.CompositeNode;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.util.Collections;
 import java.util.EnumSet;
@@ -35,54 +39,74 @@ 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<InstanceIdentifier, CompositeNode> operationalDataCache;
+    private final ConcurrentMap<InstanceIdentifier, CompositeNode> configurationDataCache;
 
-    public ClusteredDataStoreImpl(IClusterGlobalServices clusterGlobalServices) throws CacheExistException, CacheConfigException {
-        Preconditions.checkNotNull(clusterGlobalServices, "clusterGlobalServices cannot be null");
+    private Logger logger = LoggerFactory.getLogger(ClusteredDataStoreImpl.class);
 
-        operationalDataCache = clusterGlobalServices.createCache(OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
+    public ClusteredDataStoreImpl(IClusterGlobalServices clusterGlobalServices) throws CacheConfigException {
+        logger.trace("Constructing clustered data store");
+        Preconditions.checkNotNull(clusterGlobalServices, "clusterGlobalServices cannot be null");
 
-        if(operationalDataCache == null){
-            Preconditions.checkNotNull(operationalDataCache, "operationalDataCache cannot be null");
-        }
+        operationalDataCache = getOrCreateCache(clusterGlobalServices, OPERATIONAL_DATA_CACHE);
 
-        configurationDataCache = clusterGlobalServices.createCache(CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
+        Preconditions.checkNotNull(operationalDataCache, "operationalDataCache cannot be null");
 
-        if(configurationDataCache == null){
-            Preconditions.checkNotNull(configurationDataCache, "configurationDataCache cannot be null");
-        }
+        configurationDataCache = getOrCreateCache(clusterGlobalServices, CONFIGURATION_DATA_CACHE);
 
+        Preconditions.checkNotNull(configurationDataCache, "configurationDataCache cannot be null");
     }
 
     @Override
-    public DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> requestCommit(DataModification<InstanceIdentifier<? extends Object>, Object> modification) {
+    public DataCommitTransaction<InstanceIdentifier, CompositeNode> requestCommit(DataModification<InstanceIdentifier, CompositeNode> modification) {
         return new ClusteredDataStoreTransaction(modification);
     }
 
     @Override
-    public Object readOperationalData(InstanceIdentifier<? extends Object> path) {
+    public CompositeNode readOperationalData(InstanceIdentifier path) {
         Preconditions.checkNotNull(path, "path cannot be null");
         return operationalDataCache.get(path);
     }
 
     @Override
-    public Object readConfigurationData(InstanceIdentifier<? extends Object> path) {
+    public boolean containsConfigurationPath(InstanceIdentifier path) {
+        return configurationDataCache.containsKey(path);
+    }
+
+    @Override
+    public boolean containsOperationalPath(InstanceIdentifier path) {
+        return operationalDataCache.containsKey(path);
+    }
+
+    @Override
+    public Iterable<InstanceIdentifier> getStoredConfigurationPaths() {
+        return configurationDataCache.keySet();
+    }
+
+    @Override
+    public Iterable<InstanceIdentifier> getStoredOperationalPaths() {
+        return operationalDataCache.keySet();
+    }
+
+
+
+    @Override
+    public CompositeNode readConfigurationData(InstanceIdentifier path) {
         Preconditions.checkNotNull(path, "path cannot be null");
         return configurationDataCache.get(path);
     }
 
     private RpcResult<Void> finish(final ClusteredDataStoreTransaction transaction) {
-      final DataModification<InstanceIdentifier<? extends Object>,Object> modification = transaction.getModification();
+      final DataModification<InstanceIdentifier,CompositeNode> modification = transaction.getModification();
 
       this.configurationDataCache.putAll(modification.getUpdatedConfigurationData());
       this.operationalDataCache.putAll(modification.getUpdatedOperationalData());
 
-      for (final InstanceIdentifier<? extends Object> removal : modification.getRemovedConfigurationData()) {
+      for (final InstanceIdentifier removal : modification.getRemovedConfigurationData()) {
         this.configurationDataCache.remove(removal);
       }
 
-      for (final InstanceIdentifier<? extends Object> removal : modification.getRemovedOperationalData()) {
+      for (final InstanceIdentifier removal : modification.getRemovedOperationalData()) {
         this.operationalDataCache.remove(removal  );
       }
 
@@ -95,17 +119,31 @@ public class ClusteredDataStoreImpl implements ClusteredDataStore {
       return Rpcs.<Void>getRpcResult(true, null, _emptySet);
     }
 
-    private class ClusteredDataStoreTransaction implements DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> {
-        private final DataModification<InstanceIdentifier<? extends Object>,Object> modification;
 
-        public ClusteredDataStoreTransaction(DataModification<InstanceIdentifier<? extends Object>,Object> modification){
+    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, CompositeNode> {
+        private final DataModification<InstanceIdentifier,CompositeNode> modification;
+
+        public ClusteredDataStoreTransaction(DataModification<InstanceIdentifier,CompositeNode> modification){
             Preconditions.checkNotNull(modification, "modification cannot be null");
 
             this.modification = modification;
         }
 
         @Override
-        public DataModification<InstanceIdentifier<? extends Object>, Object> getModification() {
+        public DataModification<InstanceIdentifier, CompositeNode> getModification() {
             return this.modification;
         }