Refactor Register*ListenerReply classes
[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.osgi.framework.BundleContext;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class DistributedDataStoreFactory {
22     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStoreFactory.class);
23
24     public static AbstractDataStore createInstance(final SchemaService schemaService,
25             final DatastoreContext initialDatastoreContext, final DatastoreSnapshotRestore datastoreSnapshotRestore,
26             final ActorSystemProvider actorSystemProvider, final BundleContext bundleContext) {
27
28         final String datastoreName = initialDatastoreContext.getDataStoreName();
29         LOG.info("Create data store instance of type : {}", datastoreName);
30
31         final ActorSystem actorSystem = actorSystemProvider.getActorSystem();
32         final DatastoreSnapshot restoreFromSnapshot = datastoreSnapshotRestore.getAndRemove(datastoreName);
33         final DatastoreContextIntrospector introspector = new DatastoreContextIntrospector(initialDatastoreContext);
34         final DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
35                 introspector, bundleContext);
36
37         final Configuration config = new ConfigurationImpl("module-shards.conf", "modules.conf");
38         final ClusterWrapper clusterWrapper = new ClusterWrapperImpl(actorSystem);
39         final DatastoreContextFactory contextFactory = introspector.newContextFactory();
40
41         // This is the potentially-updated datastore context, distinct from the initial one
42         final DatastoreContext datastoreContext = contextFactory.getBaseDatastoreContext();
43
44         final AbstractDataStore dataStore;
45         if (datastoreContext.isUseTellBasedProtocol()) {
46             dataStore = new ClientBackedDataStore(actorSystem, clusterWrapper, config, contextFactory,
47                 restoreFromSnapshot);
48             LOG.info("Data store {} is using tell-based protocol", datastoreName);
49         } else {
50             dataStore = new DistributedDataStore(actorSystem, clusterWrapper, config, contextFactory,
51                 restoreFromSnapshot);
52             LOG.info("Data store {} is using ask-based protocol", datastoreName);
53         }
54
55         overlay.setListener(dataStore);
56
57         schemaService.registerSchemaContextListener(dataStore);
58
59         dataStore.setCloseable(overlay);
60         dataStore.waitTillReady();
61
62         return dataStore;
63     }
64 }