Removing the redundant configuration reader in the distributed datastore.
[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.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;
25
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);
32
33     public static synchronized DistributedDataStore createInstance(SchemaService schemaService,
34             DatastoreContext datastoreContext, BundleContext bundleContext) {
35
36         LOG.info("Create data store instance of type : {}", datastoreContext.getDataStoreType());
37
38         DatastoreContextIntrospector introspector = new DatastoreContextIntrospector(datastoreContext);
39         DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
40                 introspector, bundleContext);
41
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());
46
47         overlay.setListener(dataStore);
48
49         ShardStrategyFactory.setConfiguration(config);
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 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.