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
8 package org.opendaylight.controller.cluster.datastore;
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;
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";
23 private static volatile ActorSystem persistentActorSystem = null;
25 public static DistributedDataStore createInstance(SchemaService schemaService,
26 DatastoreContext datastoreContext, BundleContext bundleContext) {
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);
34 ShardStrategyFactory.setConfiguration(config);
35 schemaService.registerSchemaContextListener(dataStore);
39 private static final ActorSystem getOrCreateInstance(final BundleContext bundleContext, ConfigurationReader configurationReader) {
40 ActorSystem ret = persistentActorSystem;
42 synchronized (DistributedDataStoreFactory.class) {
43 ret = persistentActorSystem;
45 // Create an OSGi bundle classloader for actor system
46 BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle(),
47 Thread.currentThread().getContextClassLoader());
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");
53 persistentActorSystem = ret;