Merge "BUG 2412 - restconf @GET getModule(identifier,uri) method 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 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, AutoCloseable {
38
39     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
40     public static final int REGISTER_DATA_CHANGE_LISTENER_TIMEOUT_FACTOR = 24; // 24 times the usual operation timeout
41
42     private final ActorContext actorContext;
43
44     private AutoCloseable closeable;
45
46     private DatastoreConfigurationMXBeanImpl datastoreConfigMXBean;
47
48     public DistributedDataStore(ActorSystem actorSystem, ClusterWrapper cluster,
49             Configuration configuration, DatastoreContext datastoreContext) {
50         Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
51         Preconditions.checkNotNull(cluster, "cluster should not be null");
52         Preconditions.checkNotNull(configuration, "configuration should not be null");
53         Preconditions.checkNotNull(datastoreContext, "datastoreContext should not be null");
54
55         String type = datastoreContext.getDataStoreType();
56
57         String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
58
59         LOG.info("Creating ShardManager : {}", shardManagerId);
60
61         String shardDispatcher =
62                 new Dispatchers(actorSystem.dispatchers()).getDispatcherPath(Dispatchers.DispatcherType.Shard);
63
64         actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
65                 ShardManager.props(cluster, configuration, datastoreContext)
66                         .withDispatcher(shardDispatcher).withMailbox(ActorContext.MAILBOX), shardManagerId ),
67                 cluster, configuration, datastoreContext);
68
69         datastoreConfigMXBean = new DatastoreConfigurationMXBeanImpl(datastoreContext.getDataStoreMXBeanType());
70         datastoreConfigMXBean.setContext(datastoreContext);
71         datastoreConfigMXBean.registerMBean();
72     }
73
74     public DistributedDataStore(ActorContext actorContext) {
75         this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
76     }
77
78     public void setCloseable(AutoCloseable closeable) {
79         this.closeable = closeable;
80     }
81
82     @SuppressWarnings("unchecked")
83     @Override
84     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
85                                               ListenerRegistration<L> registerChangeListener(
86         final YangInstanceIdentifier path, L listener,
87         AsyncDataBroker.DataChangeScope scope) {
88
89         Preconditions.checkNotNull(path, "path should not be null");
90         Preconditions.checkNotNull(listener, "listener should not be null");
91
92         LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
93
94         String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
95
96         final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
97                 new DataChangeListenerRegistrationProxy(shardName, actorContext, listener);
98         listenerRegistrationProxy.init(path, scope);
99
100         return listenerRegistrationProxy;
101     }
102
103     @Override
104     public DOMStoreTransactionChain createTransactionChain() {
105         return new TransactionChainProxy(actorContext);
106     }
107
108     @Override
109     public DOMStoreReadTransaction newReadOnlyTransaction() {
110         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
111     }
112
113     @Override
114     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
115         actorContext.acquireTxCreationPermit();
116         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
117     }
118
119     @Override
120     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
121         actorContext.acquireTxCreationPermit();
122         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
123     }
124
125     @Override
126     public void onGlobalContextUpdated(SchemaContext schemaContext) {
127         actorContext.setSchemaContext(schemaContext);
128     }
129
130     @Override
131     public void close() {
132         datastoreConfigMXBean.unregisterMBean();
133
134         if(closeable != null) {
135             try {
136                 closeable.close();
137             } catch (Exception e) {
138                 LOG.debug("Error closing insance", e);
139             }
140         }
141
142         actorContext.shutdown();
143     }
144
145     @VisibleForTesting
146     ActorContext getActorContext() {
147         return actorContext;
148     }
149 }