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