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.google.common.base.Preconditions;
14 import com.typesafe.config.ConfigFactory;
15 import java.util.HashSet;
17 import java.util.concurrent.TimeUnit;
18 import org.opendaylight.controller.cluster.common.actor.AkkaConfigurationReader;
19 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
20 import org.opendaylight.controller.sal.core.api.model.SchemaService;
21 import org.osgi.framework.BundleContext;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import scala.concurrent.duration.Duration;
26 public class DistributedDataStoreFactory {
27 private static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
28 private static final String CONFIGURATION_NAME = "odl-cluster-data";
29 private static ActorSystem actorSystem = null;
30 private static final Set<DistributedDataStore> createdInstances = new HashSet<>(2);
31 private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStoreFactory.class);
33 public static synchronized DistributedDataStore createInstance(SchemaService schemaService,
34 DatastoreContext datastoreContext, BundleContext bundleContext) {
36 LOG.info("Create data store instance of type : {}", datastoreContext.getDataStoreType());
38 DatastoreContextIntrospector introspector = new DatastoreContextIntrospector(datastoreContext);
39 DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
40 introspector, bundleContext);
42 ActorSystem actorSystem = getActorSystem(bundleContext, datastoreContext.getConfigurationReader());
43 Configuration config = new ConfigurationImpl("module-shards.conf", "modules.conf");
44 final DistributedDataStore dataStore = new DistributedDataStore(actorSystem,
45 new ClusterWrapperImpl(actorSystem), config, introspector.getContext());
47 overlay.setListener(dataStore);
49 ShardStrategyFactory.setConfiguration(config);
50 schemaService.registerSchemaContextListener(dataStore);
52 dataStore.setCloseable(overlay);
53 dataStore.waitTillReady();
55 createdInstances.add(dataStore);
59 private static synchronized final ActorSystem getActorSystem(final BundleContext bundleContext,
60 AkkaConfigurationReader configurationReader) {
61 if (actorSystem == null) {
62 // Create an OSGi bundle classloader for actor system
63 BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle(),
64 Thread.currentThread().getContextClassLoader());
66 actorSystem = ActorSystem.create(ACTOR_SYSTEM_NAME,
67 ConfigFactory.load(configurationReader.read()).getConfig(CONFIGURATION_NAME), classLoader);
68 actorSystem.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
74 public static synchronized void destroyInstance(DistributedDataStore dataStore){
75 Preconditions.checkNotNull(dataStore, "dataStore should not be null");
77 LOG.info("Destroy data store instance of type : {}", dataStore.getActorContext().getDataStoreType());
79 if(createdInstances.remove(dataStore)){
80 if(createdInstances.size() == 0){
81 if(actorSystem != null) {
82 actorSystem.shutdown();
84 actorSystem.awaitTermination(Duration.create(10, TimeUnit.SECONDS));
85 } catch (Exception e) {
86 LOG.warn("Error awaiting actor termination", e);