Merge "BUG 2412 - restconf RestconfDocumentedExceptionMapper class migration"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DistributedDataStore.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
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.actor.ActorSystem;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.Preconditions;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.TimeUnit;
16 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
17 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.DatastoreConfigurationMXBeanImpl;
18 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
19 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
20 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
22 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  *
38  */
39 public class DistributedDataStore implements DOMStore, SchemaContextListener,
40         DatastoreContextConfigAdminOverlay.Listener, AutoCloseable {
41
42     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
43     private static final String UNKNOWN_TYPE = "unknown";
44
45     private static final long READY_WAIT_FACTOR = 3;
46
47     private final ActorContext actorContext;
48     private final long waitTillReadyTimeInMillis;
49
50
51     private AutoCloseable closeable;
52
53     private DatastoreConfigurationMXBeanImpl datastoreConfigMXBean;
54
55     private CountDownLatch waitTillReadyCountDownLatch = new CountDownLatch(1);
56
57     private final String type;
58
59     public DistributedDataStore(ActorSystem actorSystem, ClusterWrapper cluster,
60             Configuration configuration, DatastoreContext datastoreContext) {
61         Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
62         Preconditions.checkNotNull(cluster, "cluster should not be null");
63         Preconditions.checkNotNull(configuration, "configuration should not be null");
64         Preconditions.checkNotNull(datastoreContext, "datastoreContext should not be null");
65
66         this.type = datastoreContext.getDataStoreType();
67
68         String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
69
70         LOG.info("Creating ShardManager : {}", shardManagerId);
71
72         String shardDispatcher =
73                 new Dispatchers(actorSystem.dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
74
75         actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
76                 ShardManager.props(cluster, configuration, datastoreContext, waitTillReadyCountDownLatch)
77                         .withDispatcher(shardDispatcher).withMailbox(ActorContext.MAILBOX), shardManagerId ),
78                 cluster, configuration, datastoreContext);
79
80         this.waitTillReadyTimeInMillis =
81                 actorContext.getDatastoreContext().getShardLeaderElectionTimeout().duration().toMillis() * READY_WAIT_FACTOR;
82
83
84         datastoreConfigMXBean = new DatastoreConfigurationMXBeanImpl(datastoreContext.getDataStoreMXBeanType());
85         datastoreConfigMXBean.setContext(datastoreContext);
86         datastoreConfigMXBean.registerMBean();
87     }
88
89     public DistributedDataStore(ActorContext actorContext) {
90         this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
91         this.type = UNKNOWN_TYPE;
92         this.waitTillReadyTimeInMillis =
93                 actorContext.getDatastoreContext().getShardLeaderElectionTimeout().duration().toMillis() * READY_WAIT_FACTOR;
94
95     }
96
97     public void setCloseable(AutoCloseable closeable) {
98         this.closeable = closeable;
99     }
100
101     @SuppressWarnings("unchecked")
102     @Override
103     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
104                                               ListenerRegistration<L> registerChangeListener(
105         final YangInstanceIdentifier path, L listener,
106         AsyncDataBroker.DataChangeScope scope) {
107
108         Preconditions.checkNotNull(path, "path should not be null");
109         Preconditions.checkNotNull(listener, "listener should not be null");
110
111         LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
112
113         String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
114
115         final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
116                 new DataChangeListenerRegistrationProxy(shardName, actorContext, listener);
117         listenerRegistrationProxy.init(path, scope);
118
119         return listenerRegistrationProxy;
120     }
121
122     @Override
123     public DOMStoreTransactionChain createTransactionChain() {
124         return new TransactionChainProxy(actorContext);
125     }
126
127     @Override
128     public DOMStoreReadTransaction newReadOnlyTransaction() {
129         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
130     }
131
132     @Override
133     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
134         actorContext.acquireTxCreationPermit();
135         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
136     }
137
138     @Override
139     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
140         actorContext.acquireTxCreationPermit();
141         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
142     }
143
144     @Override
145     public void onGlobalContextUpdated(SchemaContext schemaContext) {
146         actorContext.setSchemaContext(schemaContext);
147     }
148
149     @Override
150     public void onDatastoreContextUpdated(DatastoreContext context) {
151         LOG.info("DatastoreContext updated for data store {}", actorContext.getDataStoreType());
152
153         actorContext.setDatastoreContext(context);
154         datastoreConfigMXBean.setContext(context);
155     }
156
157     @Override
158     public void close() {
159         datastoreConfigMXBean.unregisterMBean();
160
161         if(closeable != null) {
162             try {
163                 closeable.close();
164             } catch (Exception e) {
165                 LOG.debug("Error closing insance", e);
166             }
167         }
168
169         actorContext.shutdown();
170     }
171
172     @VisibleForTesting
173     ActorContext getActorContext() {
174         return actorContext;
175     }
176
177     public void waitTillReady(){
178         LOG.info("Beginning to wait for data store to become ready : {}", type);
179
180         try {
181             waitTillReadyCountDownLatch.await(waitTillReadyTimeInMillis, TimeUnit.MILLISECONDS);
182
183             LOG.debug("Data store {} is now ready", type);
184         } catch (InterruptedException e) {
185             LOG.error("Interrupted when trying to wait for shards to become leader in a reasonable amount of time - giving up");
186         }
187     }
188
189     @VisibleForTesting
190     public CountDownLatch getWaitTillReadyCountDownLatch() {
191         return waitTillReadyCountDownLatch;
192     }
193 }