Merge "Startup arch - remove artifactId prefix from dir names."
[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         ActorSystem actorSystem = getOrCreateInstance(bundleContext, datastoreContext.getConfigurationReader());
29         Configuration config = new ConfigurationImpl("module-shards.conf", "modules.conf");
30         final DistributedDataStore dataStore =
31                 new DistributedDataStore(actorSystem, new ClusterWrapperImpl(actorSystem),
32                         config, datastoreContext);
33
34         ShardStrategyFactory.setConfiguration(config);
35         schemaService.registerSchemaContextListener(dataStore);
36         return dataStore;
37     }
38
39     private static final ActorSystem getOrCreateInstance(final BundleContext bundleContext, ConfigurationReader configurationReader) {
40         ActorSystem ret = persistentActorSystem;
41         if (ret == null) {
42             synchronized (DistributedDataStoreFactory.class) {
43                 ret = persistentActorSystem;
44                 if (ret == null) {
45                     // Create an OSGi bundle classloader for actor system
46                     BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle(),
47                         Thread.currentThread().getContextClassLoader());
48
49                     ret = ActorSystem.create(ACTOR_SYSTEM_NAME,
50                         ConfigFactory.load(configurationReader.read()).getConfig(CONFIGURATION_NAME), classLoader);
51                     ret.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
52
53                     persistentActorSystem = ret;
54                 }
55             }
56         }
57
58         return ret;
59     }
60 }