Configuration Save event sync mechanism using Clustering services.
[controller.git] / opendaylight / configuration / implementation / src / main / java / org / opendaylight / controller / configuration / internal / ConfigurationImpl.java
index 20821f10282b038c19202a0f2c9d82839072b08b..8e2741e7d1b1231b5d16ab63af07712ea4a8cad9 100644 (file)
 package org.opendaylight.controller.configuration.internal;
 
 import java.util.Collections;
+import java.util.EnumSet;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.concurrent.ConcurrentMap;
 
+import org.opendaylight.controller.clustering.services.CacheConfigException;
+import org.opendaylight.controller.clustering.services.CacheExistException;
+import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
+import org.opendaylight.controller.clustering.services.IClusterServices;
+import org.opendaylight.controller.configuration.ConfigurationEvent;
 import org.opendaylight.controller.configuration.IConfigurationAware;
 import org.opendaylight.controller.configuration.IConfigurationService;
 import org.opendaylight.controller.sal.utils.StatusCode;
@@ -29,10 +36,11 @@ import org.slf4j.LoggerFactory;
  *
  */
 
-public class ConfigurationImpl implements IConfigurationService {
+public class ConfigurationImpl implements IConfigurationService, ICacheUpdateAware<ConfigurationEvent, String> {
     private static final Logger logger = LoggerFactory
             .getLogger(ConfigurationImpl.class);
     private IClusterGlobalServices clusterServices;
+    private ConcurrentMap <ConfigurationEvent, String> configEvent;
     /*
      * Collection containing the configuration objects.
      * This is configuration world: container names (also the map key)
@@ -72,6 +80,11 @@ public class ConfigurationImpl implements IConfigurationService {
         logger.info("ContainerManager startup....");
     }
 
+    public void start() {
+        allocateCache();
+        retrieveCache();
+    }
+
     public void destroy() {
         // Clear local states
         this.configurationAwareList.clear();
@@ -79,21 +92,76 @@ public class ConfigurationImpl implements IConfigurationService {
 
     @Override
     public Status saveConfigurations() {
+        if (configEvent != null) {
+            configEvent.put(ConfigurationEvent.SAVE, "");
+        }
+        return saveConfigurationsInternal();
+    }
+
+    private Status saveConfigurationsInternal() {
         boolean success = true;
         for (IConfigurationAware configurationAware : configurationAwareList) {
-                Status status = configurationAware.saveConfiguration();
+            Status status = configurationAware.saveConfiguration();
             if (!status.isSuccess()) {
                 success = false;
                 logger.info("Failed to save config for {}",
-                                configurationAware.getClass().getName());
+                        configurationAware.getClass().getName());
             }
         }
         if (success) {
-            return new Status(StatusCode.SUCCESS, null);
+            return new Status(StatusCode.SUCCESS);
         } else {
             return new Status(StatusCode.INTERNALERROR,
-                        "Failed to Save All Configurations");
+                    "Failed to Save All Configurations");
+        }
+    }
+
+    @Override
+    public void entryCreated(ConfigurationEvent key, String cacheName,
+            boolean originLocal) {
+        if (originLocal) return;
+    }
+
+    @Override
+    public void entryUpdated(ConfigurationEvent key, String new_value,
+            String cacheName, boolean originLocal) {
+        if (originLocal) return;
+        if (key == ConfigurationEvent.SAVE) {
+            saveConfigurationsInternal();
         }
     }
 
+    @Override
+    public void entryDeleted(ConfigurationEvent key, String cacheName,
+            boolean originLocal) {
+        if (originLocal) return;
+    }
+
+    @SuppressWarnings("deprecation")
+    private void allocateCache() {
+        if (this.clusterServices == null) {
+            logger.error("uninitialized clusterServices, can't create cache");
+            return;
+        }
+        try {
+            this.clusterServices.createCache("config.event.save",
+                    EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
+        } catch (CacheConfigException cce) {
+            logger.error("Error creating Configuration cache ", cce);
+        } catch (CacheExistException cce) {
+            logger.error("Configuration Cache already exists, destroy and recreate ", cce);
+        }
+    }
+
+    @SuppressWarnings({ "unchecked", "deprecation" })
+    private void retrieveCache() {
+        if (this.clusterServices == null) {
+            logger.error("uninitialized clusterServices, can't retrieve cache");
+            return;
+        }
+        configEvent = (ConcurrentMap<ConfigurationEvent, String>) this.clusterServices.getCache("config.event.save");
+        if (configEvent == null) {
+            logger.error("Failed to retrieve configuration Cache");
+        }
+    }
 }