Merge "Fixed test which tested incorrect string formating"
[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.google.common.base.Preconditions;
15 import com.typesafe.config.Config;
16 import com.typesafe.config.ConfigFactory;
17 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
18 import org.opendaylight.controller.sal.core.api.model.SchemaService;
19 import org.osgi.framework.BundleContext;
20
21 import java.io.File;
22 import java.util.concurrent.atomic.AtomicReference;
23
24 public class DistributedDataStoreFactory {
25
26     public static final String AKKA_CONF_PATH = "./configuration/initial/akka.conf";
27     public static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
28     public static final String CONFIGURATION_NAME = "odl-cluster-data";
29     private static AtomicReference<ActorSystem> actorSystem = new AtomicReference<>();
30
31     public static DistributedDataStore createInstance(String name, SchemaService schemaService,
32                                                       DatastoreContext datastoreContext, BundleContext bundleContext) {
33
34         ActorSystem actorSystem = getOrCreateInstance(bundleContext);
35         Configuration config = new ConfigurationImpl("module-shards.conf", "modules.conf");
36         final DistributedDataStore dataStore =
37                 new DistributedDataStore(actorSystem, name, new ClusterWrapperImpl(actorSystem),
38                         config, datastoreContext);
39
40         ShardStrategyFactory.setConfiguration(config);
41         schemaService.registerSchemaContextListener(dataStore);
42         return dataStore;
43     }
44
45     synchronized private static final ActorSystem getOrCreateInstance(final BundleContext bundleContext) {
46
47         if (actorSystem.get() != null){
48             return actorSystem.get();
49         }
50         // Create an OSGi bundle classloader for actor system
51         BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle(),
52                 Thread.currentThread().getContextClassLoader());
53
54         ActorSystem system = ActorSystem.create(ACTOR_SYSTEM_NAME,
55                 ConfigFactory.load(readAkkaConfiguration()).getConfig(CONFIGURATION_NAME), classLoader);
56         system.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
57
58         actorSystem.set(system);
59         return system;
60     }
61
62
63     private static final Config readAkkaConfiguration() {
64         File defaultConfigFile = new File(AKKA_CONF_PATH);
65         Preconditions.checkState(defaultConfigFile.exists(), "akka.conf is missing");
66         return ConfigFactory.parseFile(defaultConfigFile);
67     }
68 }