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