Introduce ASYNC caches and use them in FRM
[controller.git] / opendaylight / clustering / services_implementation / src / main / java / org / opendaylight / controller / clustering / services_implementation / internal / ClusterManager.java
index c3fd30ae9b65629c8e83bb7f789229b0b6bc5870..122063abba0e3c92ad38c2af28a6a9e72f1dcf86 100644 (file)
@@ -33,6 +33,10 @@ import javax.transaction.TransactionManager;
 
 import org.infinispan.Cache;
 import org.infinispan.configuration.cache.Configuration;
+import org.infinispan.configuration.cache.ConfigurationBuilder;
+import org.infinispan.configuration.global.GlobalConfigurationBuilder;
+import org.infinispan.configuration.parsing.ConfigurationBuilderHolder;
+import org.infinispan.configuration.parsing.ParserRegistry;
 import org.infinispan.manager.DefaultCacheManager;
 import org.infinispan.manager.EmbeddedCacheManager;
 import org.infinispan.notifications.Listener;
@@ -52,11 +56,10 @@ import org.opendaylight.controller.clustering.services.IClusterServices;
 import org.opendaylight.controller.clustering.services.IGetUpdates;
 import org.opendaylight.controller.clustering.services.IListenRoleChange;
 import org.opendaylight.controller.clustering.services.ListenRoleChangeAddException;
-import org.opendaylight.controller.sal.core.IContainerAware;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class ClusterManager implements IClusterServices, IContainerAware {
+public class ClusterManager implements IClusterServices {
     protected static final Logger logger = LoggerFactory
             .getLogger(ClusterManager.class);
     private DefaultCacheManager cm;
@@ -227,7 +230,8 @@ public class ClusterManager implements IClusterServices, IContainerAware {
         if (amIGossipRouter) {
             logger.info("I'm a GossipRouter will listen on port {}",
                     gossipRouterPort);
-            res = new GossipRouter(gossipRouterPort);
+            // Start a GossipRouter with JMX support
+            res = new GossipRouter(gossipRouterPort, null, true);
         }
         return res;
     }
@@ -246,8 +250,17 @@ public class ClusterManager implements IClusterServices, IContainerAware {
         }
         logger.info("Starting the ClusterManager");
         try {
-            //FIXME keeps throwing FileNotFoundException
-            this.cm = new DefaultCacheManager("config/infinispan-config.xml");
+            ParserRegistry parser = new ParserRegistry(this.getClass()
+                    .getClassLoader());
+            String infinispanConfigFile =
+                    System.getProperty("org.infinispan.config.file", "config/infinispan-config.xml");
+            logger.debug("Using configuration file:{}", infinispanConfigFile);
+            ConfigurationBuilderHolder holder = parser.parseFile(infinispanConfigFile);
+            GlobalConfigurationBuilder globalBuilder = holder.getGlobalConfigurationBuilder();
+            globalBuilder.serialization()
+                    .classResolver(new ClassResolver())
+                    .build();
+            this.cm = new DefaultCacheManager(holder, false);
             logger.debug("Allocated ClusterManager");
             if (this.cm != null) {
                 this.cm.start();
@@ -292,24 +305,54 @@ public class ClusterManager implements IClusterServices, IContainerAware {
             throw new CacheExistException();
         }
 
-        // Sanity check to avoid contrasting parameters
-        if (cMode.containsAll(EnumSet.of(
-                IClusterServices.cacheMode.NON_TRANSACTIONAL,
+        // Sanity check to avoid contrasting parameters between transactional
+        // and not
+        if (cMode.containsAll(EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL,
                 IClusterServices.cacheMode.TRANSACTIONAL))) {
             throw new CacheConfigException();
         }
 
-        if (cMode.contains(IClusterServices.cacheMode.NON_TRANSACTIONAL)) {
-            c = manager.getCache(realCacheName);
-            return c;
-        } else if (cMode.contains(IClusterServices.cacheMode.TRANSACTIONAL)) {
-            Configuration rc = manager
-                    .getCacheConfiguration("transactional-type");
-            manager.defineConfiguration(realCacheName, rc);
-            c = manager.getCache(realCacheName);
-            return c;
+        // Sanity check to avoid contrasting parameters between sync and async
+        if (cMode.containsAll(EnumSet.of(IClusterServices.cacheMode.SYNC, IClusterServices.cacheMode.ASYNC))) {
+            throw new CacheConfigException();
         }
-        return null;
+
+        Configuration fromTemplateConfig = null;
+        /*
+         * Fetch transactional/non-transactional templates
+         */
+        // Check if transactional
+        if (cMode.contains(IClusterServices.cacheMode.TRANSACTIONAL)) {
+            fromTemplateConfig = manager.getCacheConfiguration("transactional-type");
+        } else if (cMode.contains(IClusterServices.cacheMode.NON_TRANSACTIONAL)) {
+            fromTemplateConfig = manager.getDefaultCacheConfiguration();
+        }
+
+        // If none set the transactional property then just return null
+        if (fromTemplateConfig == null) {
+            return null;
+        }
+
+        ConfigurationBuilder builder = new ConfigurationBuilder();
+        builder.read(fromTemplateConfig);
+        /*
+         * Now evaluate async/sync
+         */
+        if (cMode.contains(IClusterServices.cacheMode.ASYNC)) {
+            builder.clustering()
+                    .cacheMode(fromTemplateConfig.clustering()
+                            .cacheMode()
+                            .toAsync());
+        } else if (cMode.contains(IClusterServices.cacheMode.SYNC)) {
+            builder.clustering()
+                    .cacheMode(fromTemplateConfig.clustering()
+                            .cacheMode()
+                            .toSync());
+        }
+
+        manager.defineConfiguration(realCacheName, builder.build());
+        c = manager.getCache(realCacheName);
+        return c;
     }
 
     @Override
@@ -358,6 +401,7 @@ public class ClusterManager implements IClusterServices, IContainerAware {
             return null;
         }
         for (String cacheName : manager.getCacheNames()) {
+            if (!manager.isRunning(cacheName)) continue;
             if (cacheName.startsWith("{" + containerName + "}_")) {
                 String[] res = cacheName.split("[{}]");
                 if (res.length >= 4 && res[1].equals(containerName)
@@ -663,21 +707,4 @@ public class ClusterManager implements IClusterServices, IContainerAware {
             }
         }
     }
-
-    private void removeContainerCaches(String containerName) {
-        logger.info("Destroying caches for container {}", containerName);
-        for (String cacheName : this.getCacheList(containerName)) {
-            this.destroyCache(containerName, cacheName);
-        }
-    }
-
-    @Override
-    public void containerCreate(String arg0) {
-        // no op
-    }
-
-    @Override
-    public void containerDestroy(String container) {
-        removeContainerCaches(container);
-    }
 }