BUG 2762 : Compute rate limit based on a more lenient algorithm
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DistributedDataStoreFactory.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import akka.actor.ActorSystem;
11 import akka.actor.Props;
12 import akka.osgi.BundleDelegatingClassLoader;
13 import com.typesafe.config.ConfigFactory;
14 import org.opendaylight.controller.cluster.datastore.config.ConfigurationReader;
15 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
16 import org.opendaylight.controller.sal.core.api.model.SchemaService;
17 import org.osgi.framework.BundleContext;
18
19 public class DistributedDataStoreFactory {
20     private static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
21     private static final String CONFIGURATION_NAME = "odl-cluster-data";
22
23     private static volatile ActorSystem persistentActorSystem = null;
24
25     public static DistributedDataStore createInstance(SchemaService schemaService,
26             DatastoreContext datastoreContext, BundleContext bundleContext) {
27
28         DatastoreContextIntrospector introspector = new DatastoreContextIntrospector(datastoreContext);
29         DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
30                 introspector, bundleContext);
31
32         ActorSystem actorSystem = getOrCreateInstance(bundleContext, datastoreContext.getConfigurationReader());
33         Configuration config = new ConfigurationImpl("module-shards.conf", "modules.conf");
34         final DistributedDataStore dataStore = new DistributedDataStore(actorSystem,
35                 new ClusterWrapperImpl(actorSystem), config, introspector.getContext());
36
37         ShardStrategyFactory.setConfiguration(config);
38         schemaService.registerSchemaContextListener(dataStore);
39
40         dataStore.setCloseable(overlay);
41         return dataStore;
42     }
43
44     private static final ActorSystem getOrCreateInstance(final BundleContext bundleContext, ConfigurationReader configurationReader) {
45         ActorSystem ret = persistentActorSystem;
46         if (ret == null) {
47             synchronized (DistributedDataStoreFactory.class) {
48                 ret = persistentActorSystem;
49                 if (ret == null) {
50                     // Create an OSGi bundle classloader for actor system
51                     BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle(),
52                         Thread.currentThread().getContextClassLoader());
53
54                     ret = ActorSystem.create(ACTOR_SYSTEM_NAME,
55                         ConfigFactory.load(configurationReader.read()).getConfig(CONFIGURATION_NAME), classLoader);
56                     ret.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
57
58                     persistentActorSystem = ret;
59                 }
60             }
61         }
62
63         return ret;
64     }
65 }