Bug 8994 - FileModuleShardConfigProvider should not use hard-coded paths
[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 org.opendaylight.controller.cluster.ActorSystemProvider;
12 import org.opendaylight.controller.cluster.databroker.ClientBackedDataStore;
13 import org.opendaylight.controller.cluster.datastore.config.Configuration;
14 import org.opendaylight.controller.cluster.datastore.config.ConfigurationImpl;
15 import org.opendaylight.controller.cluster.datastore.persisted.DatastoreSnapshot;
16 import org.opendaylight.controller.sal.core.api.model.SchemaService;
17 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
18 import org.osgi.framework.BundleContext;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class DistributedDataStoreFactory {
23     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStoreFactory.class);
24     private static final String DEFAULT_MODULE_SHARDS_PATH = "./configuration/initial/module-shards.conf";
25     private static final String DEFAULT_MODULES_PATH = "./configuration/initial/modules.conf";
26
27     /**
28      * Create a data store instance.
29      *
30      * @deprecated Use {@link #createInstance(DOMSchemaService, DatastoreContext, DatastoreSnapshotRestore,
31      *                        ActorSystemProvider, BundleContext)} instead.
32      */
33     @Deprecated
34     public static AbstractDataStore createInstance(final SchemaService schemaService,
35             final DatastoreContext initialDatastoreContext, final DatastoreSnapshotRestore datastoreSnapshotRestore,
36             final ActorSystemProvider actorSystemProvider, final BundleContext bundleContext) {
37
38         return createInstance(schemaService, initialDatastoreContext, datastoreSnapshotRestore,
39                 actorSystemProvider, bundleContext, null);
40     }
41
42     public static AbstractDataStore createInstance(final DOMSchemaService schemaService,
43             final DatastoreContext initialDatastoreContext, final DatastoreSnapshotRestore datastoreSnapshotRestore,
44             final ActorSystemProvider actorSystemProvider, final BundleContext bundleContext) {
45         return createInstance(schemaService, initialDatastoreContext, datastoreSnapshotRestore, actorSystemProvider,
46                 bundleContext, null);
47     }
48
49     public static AbstractDataStore createInstance(final DOMSchemaService schemaService,
50             final DatastoreContext initialDatastoreContext, final DatastoreSnapshotRestore datastoreSnapshotRestore,
51             final ActorSystemProvider actorSystemProvider, final BundleContext bundleContext,
52             final Configuration orgConfig) {
53
54         final String datastoreName = initialDatastoreContext.getDataStoreName();
55         LOG.info("Create data store instance of type : {}", datastoreName);
56
57         final ActorSystem actorSystem = actorSystemProvider.getActorSystem();
58         final DatastoreSnapshot restoreFromSnapshot = datastoreSnapshotRestore.getAndRemove(datastoreName);
59         final DatastoreContextIntrospector introspector = new DatastoreContextIntrospector(initialDatastoreContext);
60         final DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
61                 introspector, bundleContext);
62
63         Configuration config;
64         if (orgConfig == null) {
65             config = new ConfigurationImpl(DEFAULT_MODULE_SHARDS_PATH, DEFAULT_MODULES_PATH);
66         } else {
67             config = orgConfig;
68         }
69         final ClusterWrapper clusterWrapper = new ClusterWrapperImpl(actorSystem);
70         final DatastoreContextFactory contextFactory = introspector.newContextFactory();
71
72         // This is the potentially-updated datastore context, distinct from the initial one
73         final DatastoreContext datastoreContext = contextFactory.getBaseDatastoreContext();
74
75         final AbstractDataStore dataStore;
76         if (datastoreContext.isUseTellBasedProtocol()) {
77             dataStore = new ClientBackedDataStore(actorSystem, clusterWrapper, config, contextFactory,
78                 restoreFromSnapshot);
79             LOG.info("Data store {} is using tell-based protocol", datastoreName);
80         } else {
81             dataStore = new DistributedDataStore(actorSystem, clusterWrapper, config, contextFactory,
82                 restoreFromSnapshot);
83             LOG.info("Data store {} is using ask-based protocol", datastoreName);
84         }
85
86         overlay.setListener(dataStore);
87
88         schemaService.registerSchemaContextListener(dataStore);
89
90         dataStore.setCloseable(overlay);
91         dataStore.waitTillReady();
92
93         return dataStore;
94     }
95 }