Merge "Fixed discard-changes for mdsal netconf, mapping code cleanup."
[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 org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
15 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.DatastoreConfigurationMXBeanImpl;
16 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
17 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
18 import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  *
36  */
37 public class DistributedDataStore implements DOMStore, SchemaContextListener,
38         DatastoreContextConfigAdminOverlay.Listener, AutoCloseable {
39
40     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
41     public static final int REGISTER_DATA_CHANGE_LISTENER_TIMEOUT_FACTOR = 24; // 24 times the usual operation timeout
42
43     private final ActorContext actorContext;
44
45     private AutoCloseable closeable;
46
47     private DatastoreConfigurationMXBeanImpl datastoreConfigMXBean;
48
49     public DistributedDataStore(ActorSystem actorSystem, ClusterWrapper cluster,
50             Configuration configuration, DatastoreContext datastoreContext) {
51         Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
52         Preconditions.checkNotNull(cluster, "cluster should not be null");
53         Preconditions.checkNotNull(configuration, "configuration should not be null");
54         Preconditions.checkNotNull(datastoreContext, "datastoreContext should not be null");
55
56         String type = datastoreContext.getDataStoreType();
57
58         String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
59
60         LOG.info("Creating ShardManager : {}", shardManagerId);
61
62         String shardDispatcher =
63                 new Dispatchers(actorSystem.dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
64
65         actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
66                 ShardManager.props(cluster, configuration, datastoreContext)
67                         .withDispatcher(shardDispatcher).withMailbox(ActorContext.MAILBOX), shardManagerId ),
68                 cluster, configuration, datastoreContext);
69
70         datastoreConfigMXBean = new DatastoreConfigurationMXBeanImpl(datastoreContext.getDataStoreMXBeanType());
71         datastoreConfigMXBean.setContext(datastoreContext);
72         datastoreConfigMXBean.registerMBean();
73     }
74
75     public DistributedDataStore(ActorContext actorContext) {
76         this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
77     }
78
79     public void setCloseable(AutoCloseable closeable) {
80         this.closeable = closeable;
81     }
82
83     @SuppressWarnings("unchecked")
84     @Override
85     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
86                                               ListenerRegistration<L> registerChangeListener(
87         final YangInstanceIdentifier path, L listener,
88         AsyncDataBroker.DataChangeScope scope) {
89
90         Preconditions.checkNotNull(path, "path should not be null");
91         Preconditions.checkNotNull(listener, "listener should not be null");
92
93         LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
94
95         String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
96
97         final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
98                 new DataChangeListenerRegistrationProxy(shardName, actorContext, listener);
99         listenerRegistrationProxy.init(path, scope);
100
101         return listenerRegistrationProxy;
102     }
103
104     @Override
105     public DOMStoreTransactionChain createTransactionChain() {
106         return new TransactionChainProxy(actorContext);
107     }
108
109     @Override
110     public DOMStoreReadTransaction newReadOnlyTransaction() {
111         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
112     }
113
114     @Override
115     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
116         actorContext.acquireTxCreationPermit();
117         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
118     }
119
120     @Override
121     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
122         actorContext.acquireTxCreationPermit();
123         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
124     }
125
126     @Override
127     public void onGlobalContextUpdated(SchemaContext schemaContext) {
128         actorContext.setSchemaContext(schemaContext);
129     }
130
131     @Override
132     public void onDatastoreContextUpdated(DatastoreContext context) {
133         LOG.info("DatastoreContext updated for data store {}", actorContext.getDataStoreType());
134
135         actorContext.setDatastoreContext(context);
136         datastoreConfigMXBean.setContext(context);
137     }
138
139     @Override
140     public void close() {
141         datastoreConfigMXBean.unregisterMBean();
142
143         if(closeable != null) {
144             try {
145                 closeable.close();
146             } catch (Exception e) {
147                 LOG.debug("Error closing insance", e);
148             }
149         }
150
151         actorContext.shutdown();
152     }
153
154     @VisibleForTesting
155     ActorContext getActorContext() {
156         return actorContext;
157     }
158 }