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.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;
20 import java.util.concurrent.atomic.AtomicReference;
22 public class DistributedDataStoreFactory {
24 public static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
26 public static final String CONFIGURATION_NAME = "odl-cluster-data";
28 private static AtomicReference<ActorSystem> persistentActorSystem = new AtomicReference<>();
30 public static DistributedDataStore createInstance(String name, SchemaService schemaService,
31 DatastoreContext datastoreContext, BundleContext bundleContext) {
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);
39 ShardStrategyFactory.setConfiguration(config);
40 schemaService.registerSchemaContextListener(dataStore);
44 synchronized private static final ActorSystem getOrCreateInstance(final BundleContext bundleContext, ConfigurationReader configurationReader) {
46 AtomicReference<ActorSystem> actorSystemReference = persistentActorSystem;
47 String configurationName = CONFIGURATION_NAME;
48 String actorSystemName = ACTOR_SYSTEM_NAME;
50 if (actorSystemReference.get() != null){
51 return actorSystemReference.get();
54 // Create an OSGi bundle classloader for actor system
55 BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle(),
56 Thread.currentThread().getContextClassLoader());
58 ActorSystem system = ActorSystem.create(actorSystemName,
59 ConfigFactory.load(configurationReader.read()).getConfig(configurationName), classLoader);
60 system.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
62 actorSystemReference.set(system);