Merge "Bug 1029: Remove dead code: p2site"
[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
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.actor.ActorSystem;
12 import akka.actor.Props;
13 import akka.osgi.BundleDelegatingClassLoader;
14 import com.typesafe.config.ConfigFactory;
15 import org.opendaylight.controller.cluster.datastore.config.ConfigurationReader;
16 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
17 import org.opendaylight.controller.sal.core.api.model.SchemaService;
18 import org.osgi.framework.BundleContext;
19
20 import java.util.concurrent.atomic.AtomicReference;
21
22 public class DistributedDataStoreFactory {
23
24     public static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
25
26     public static final String CONFIGURATION_NAME = "odl-cluster-data";
27
28     private static AtomicReference<ActorSystem> persistentActorSystem = new AtomicReference<>();
29
30     public static DistributedDataStore createInstance(String name, SchemaService schemaService,
31                                                       DatastoreContext datastoreContext, BundleContext bundleContext) {
32
33         ActorSystem actorSystem = getOrCreateInstance(bundleContext, datastoreContext.getConfigurationReader());
34         Configuration config = new ConfigurationImpl("module-shards.conf", "modules.conf");
35         final DistributedDataStore dataStore =
36                 new DistributedDataStore(actorSystem, name, new ClusterWrapperImpl(actorSystem),
37                         config, datastoreContext);
38
39         ShardStrategyFactory.setConfiguration(config);
40         schemaService.registerSchemaContextListener(dataStore);
41         return dataStore;
42     }
43
44     synchronized private static final ActorSystem getOrCreateInstance(final BundleContext bundleContext, ConfigurationReader configurationReader) {
45
46         AtomicReference<ActorSystem> actorSystemReference = persistentActorSystem;
47         String configurationName = CONFIGURATION_NAME;
48         String actorSystemName = ACTOR_SYSTEM_NAME;
49
50         if (actorSystemReference.get() != null){
51             return actorSystemReference.get();
52         }
53
54         // Create an OSGi bundle classloader for actor system
55         BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle(),
56                 Thread.currentThread().getContextClassLoader());
57
58         ActorSystem system = ActorSystem.create(actorSystemName,
59                 ConfigFactory.load(configurationReader.read()).getConfig(configurationName), classLoader);
60         system.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
61
62         actorSystemReference.set(system);
63         return system;
64     }
65
66 }