2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore;
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;
22 import java.util.concurrent.atomic.AtomicReference;
24 public class DistributedDataStoreFactory {
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<>();
31 public static DistributedDataStore createInstance(String name, SchemaService schemaService,
32 DatastoreContext datastoreContext, BundleContext bundleContext) {
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);
40 ShardStrategyFactory.setConfiguration(config);
41 schemaService.registerSchemaContextListener(dataStore);
45 synchronized private static final ActorSystem getOrCreateInstance(final BundleContext bundleContext) {
47 if (actorSystem.get() != null){
48 return actorSystem.get();
50 // Create an OSGi bundle classloader for actor system
51 BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle(),
52 Thread.currentThread().getContextClassLoader());
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");
58 actorSystem.set(system);
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);