4fd09914cdb6eb5185247bf51f6177064aba7337
[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
25     /**
26      * Create a data store instance.
27      *
28      * @deprecated Use {@link #createInstance(DOMSchemaService, DatastoreContext, DatastoreSnapshotRestore,
29      *                        ActorSystemProvider, BundleContext)} instead.
30      */
31     @Deprecated
32     public static AbstractDataStore createInstance(final SchemaService schemaService,
33             final DatastoreContext initialDatastoreContext, final DatastoreSnapshotRestore datastoreSnapshotRestore,
34             final ActorSystemProvider actorSystemProvider, final BundleContext bundleContext) {
35
36         return createInstance((DOMSchemaService) schemaService, initialDatastoreContext, datastoreSnapshotRestore,
37             actorSystemProvider, bundleContext);
38     }
39
40     public static AbstractDataStore createInstance(final DOMSchemaService schemaService,
41             final DatastoreContext initialDatastoreContext, final DatastoreSnapshotRestore datastoreSnapshotRestore,
42             final ActorSystemProvider actorSystemProvider, final BundleContext bundleContext) {
43
44         final String datastoreName = initialDatastoreContext.getDataStoreName();
45         LOG.info("Create data store instance of type : {}", datastoreName);
46
47         final ActorSystem actorSystem = actorSystemProvider.getActorSystem();
48         final DatastoreSnapshot restoreFromSnapshot = datastoreSnapshotRestore.getAndRemove(datastoreName);
49         final DatastoreContextIntrospector introspector = new DatastoreContextIntrospector(initialDatastoreContext);
50         final DatastoreContextConfigAdminOverlay overlay = new DatastoreContextConfigAdminOverlay(
51                 introspector, bundleContext);
52
53         final Configuration config = new ConfigurationImpl("module-shards.conf", "modules.conf");
54         final ClusterWrapper clusterWrapper = new ClusterWrapperImpl(actorSystem);
55         final DatastoreContextFactory contextFactory = introspector.newContextFactory();
56
57         // This is the potentially-updated datastore context, distinct from the initial one
58         final DatastoreContext datastoreContext = contextFactory.getBaseDatastoreContext();
59
60         final AbstractDataStore dataStore;
61         if (datastoreContext.isUseTellBasedProtocol()) {
62             dataStore = new ClientBackedDataStore(actorSystem, clusterWrapper, config, contextFactory,
63                 restoreFromSnapshot);
64             LOG.info("Data store {} is using tell-based protocol", datastoreName);
65         } else {
66             dataStore = new DistributedDataStore(actorSystem, clusterWrapper, config, contextFactory,
67                 restoreFromSnapshot);
68             LOG.info("Data store {} is using ask-based protocol", datastoreName);
69         }
70
71         overlay.setListener(dataStore);
72
73         schemaService.registerSchemaContextListener(dataStore);
74
75         dataStore.setCloseable(overlay);
76         dataStore.waitTillReady();
77
78         return dataStore;
79     }
80 }