Merge topic 'cleanup/composite-node'
[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.typesafe.config.ConfigFactory;
14 import org.opendaylight.controller.cluster.datastore.config.ConfigurationReader;
15 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
16 import org.opendaylight.controller.sal.core.api.model.SchemaService;
17 import org.osgi.framework.BundleContext;
18
19 public class DistributedDataStoreFactory {
20     private static final String ACTOR_SYSTEM_NAME = "opendaylight-cluster-data";
21     private static final String CONFIGURATION_NAME = "odl-cluster-data";
22
23     private static volatile ActorSystem persistentActorSystem = null;
24
25     public static DistributedDataStore createInstance(SchemaService schemaService,
26             DatastoreContext datastoreContext, BundleContext bundleContext) {
27
28         DatastoreContextIntrospector introspector = new DatastoreContextIntrospector(datastoreContext);
29         DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
30                 introspector, bundleContext);
31
32         ActorSystem actorSystem = getOrCreateInstance(bundleContext, datastoreContext.getConfigurationReader());
33         Configuration config = new ConfigurationImpl("module-shards.conf", "modules.conf");
34         final DistributedDataStore dataStore = new DistributedDataStore(actorSystem,
35                 new ClusterWrapperImpl(actorSystem), config, introspector.getContext());
36
37         overlay.setListener(dataStore);
38
39         ShardStrategyFactory.setConfiguration(config);
40         schemaService.registerSchemaContextListener(dataStore);
41
42         dataStore.setCloseable(overlay);
43         dataStore.waitTillReady();
44         return dataStore;
45     }
46
47     private static final ActorSystem getOrCreateInstance(final BundleContext bundleContext, ConfigurationReader configurationReader) {
48         ActorSystem ret = persistentActorSystem;
49         if (ret == null) {
50             synchronized (DistributedDataStoreFactory.class) {
51                 ret = persistentActorSystem;
52                 if (ret == null) {
53                     // Create an OSGi bundle classloader for actor system
54                     BundleDelegatingClassLoader classLoader = new BundleDelegatingClassLoader(bundleContext.getBundle(),
55                         Thread.currentThread().getContextClassLoader());
56
57                     ret = ActorSystem.create(ACTOR_SYSTEM_NAME,
58                         ConfigFactory.load(configurationReader.read()).getConfig(CONFIGURATION_NAME), classLoader);
59                     ret.actorOf(Props.create(TerminationMonitor.class), "termination-monitor");
60
61                     persistentActorSystem = ret;
62                 }
63             }
64         }
65
66         return ret;
67     }
68 }

©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.