Bug 4105: Add dynamic module/shard config for entity-owners shard
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DistributedDataStoreFactory.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
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;
16 import java.util.Set;
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.sal.core.api.model.SchemaService;
22 import org.osgi.framework.BundleContext;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import scala.concurrent.duration.Duration;
26
27 public class DistributedDataStoreFactory {
28     private static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
29     private static final String CONFIGURATION_NAME = "odl-cluster-data";
30     private static ActorSystem actorSystem = null;
31     private static final Set<DistributedDataStore> createdInstances = new HashSet<>(2);
32     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStoreFactory.class);
33
34     public static synchronized DistributedDataStore createInstance(SchemaService schemaService,
35             DatastoreContext datastoreContext, BundleContext bundleContext) {
36
37         LOG.info("Create data store instance of type : {}", datastoreContext.getDataStoreType());
38
39         DatastoreContextIntrospector introspector = new DatastoreContextIntrospector(datastoreContext);
40         DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
41                 introspector, bundleContext);
42
43         ActorSystem actorSystem = getActorSystem(bundleContext, datastoreContext.getConfigurationReader());
44         Configuration config = new ConfigurationImpl("module-shards.conf", "modules.conf");
45         final DistributedDataStore dataStore = new DistributedDataStore(actorSystem,
46                 new ClusterWrapperImpl(actorSystem), config, introspector.getContext());
47
48         overlay.setListener(dataStore);
49
50         schemaService.registerSchemaContextListener(dataStore);
51
52         dataStore.setCloseable(overlay);
53         dataStore.waitTillReady();
54
55         createdInstances.add(dataStore);
56         return dataStore;
57     }
58
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());
65
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");
69         }
70
71         return actorSystem;
72     }
73
74     public static synchronized void destroyInstance(DistributedDataStore dataStore){
75         Preconditions.checkNotNull(dataStore, "dataStore should not be null");
76
77         LOG.info("Destroy data store instance of type : {}", dataStore.getActorContext().getDataStoreType());
78
79         if(createdInstances.remove(dataStore)){
80             if(createdInstances.size() == 0){
81                 if(actorSystem != null) {
82                     actorSystem.shutdown();
83                     try {
84                         actorSystem.awaitTermination(Duration.create(10, TimeUnit.SECONDS));
85                     } catch (Exception e) {
86                         LOG.warn("Error awaiting actor termination", e);
87                     }
88                     actorSystem = null;
89                 }
90             }
91         }
92     }
93
94 }