e87fc55324d4c4e1d955e97ebafce4f82dddcb41
[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.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;
27
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);
34
35     public static synchronized DistributedDataStore createInstance(SchemaService schemaService,
36             DatastoreContext datastoreContext, BundleContext bundleContext) {
37
38         LOG.info("Create data store instance of type : {}", datastoreContext.getDataStoreType());
39
40         DatastoreContextIntrospector introspector = new DatastoreContextIntrospector(datastoreContext);
41         DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
42                 introspector, bundleContext);
43
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());
48
49         overlay.setListener(dataStore);
50
51         ShardStrategyFactory.setConfiguration(config);
52         schemaService.registerSchemaContextListener(dataStore);
53
54         dataStore.setCloseable(overlay);
55         dataStore.waitTillReady();
56
57         createdInstances.add(dataStore);
58         return dataStore;
59     }
60
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());
67
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");
71         }
72
73         return actorSystem;
74     }
75
76     public static synchronized void destroyInstance(DistributedDataStore dataStore){
77         Preconditions.checkNotNull(dataStore, "dataStore should not be null");
78
79         LOG.info("Destroy data store instance of type : {}", dataStore.getActorContext().getDataStoreType());
80
81         if(createdInstances.remove(dataStore)){
82             if(createdInstances.size() == 0){
83                 if(actorSystem != null) {
84                     actorSystem.shutdown();
85                     try {
86                         actorSystem.awaitTermination(Duration.create(10, TimeUnit.SECONDS));
87                     } catch (Exception e) {
88                         LOG.warn("Error awaiting actor termination", e);
89                     }
90                     actorSystem = null;
91                 }
92             }
93         }
94     }
95
96 }