Bug 1446: Add JMX stats for clustered data store
[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.ActorRef;
12 import akka.actor.ActorSystem;
13
14 import com.google.common.base.Preconditions;
15
16 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
17 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
18 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
19 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
20 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
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, AutoCloseable {
40
41     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
42
43     private final ActorContext actorContext;
44
45     public DistributedDataStore(ActorSystem actorSystem, String type, ClusterWrapper cluster,
46             Configuration configuration, DatastoreContext datastoreContext) {
47         Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
48         Preconditions.checkNotNull(type, "type should not be null");
49         Preconditions.checkNotNull(cluster, "cluster should not be null");
50         Preconditions.checkNotNull(configuration, "configuration should not be null");
51         Preconditions.checkNotNull(datastoreContext, "datastoreContext should not be null");
52
53         String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
54
55         LOG.info("Creating ShardManager : {}", shardManagerId);
56
57         actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
58                 ShardManager.props(type, cluster, configuration, datastoreContext)
59                     .withMailbox(ActorContext.MAILBOX), shardManagerId ), cluster, configuration);
60
61         actorContext.setOperationTimeout(datastoreContext.getOperationTimeoutInSeconds());
62     }
63
64     public DistributedDataStore(ActorContext actorContext) {
65         this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
66     }
67
68     @SuppressWarnings("unchecked")
69     @Override
70     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
71                                               ListenerRegistration<L> registerChangeListener(
72         YangInstanceIdentifier path, L listener,
73         AsyncDataBroker.DataChangeScope scope) {
74
75         Preconditions.checkNotNull(path, "path should not be null");
76         Preconditions.checkNotNull(listener, "listener should not be null");
77
78         LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
79
80         ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
81             DataChangeListener.props(listener ));
82
83         String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
84
85         Object result = actorContext.executeLocalShardOperation(shardName,
86             new RegisterChangeListener(path, dataChangeListenerActor.path(), scope));
87
88         if (result != null) {
89             RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
90             return new DataChangeListenerRegistrationProxy(actorContext
91                 .actorSelection(reply.getListenerRegistrationPath()), listener,
92                 dataChangeListenerActor);
93         }
94
95         LOG.debug(
96             "No local shard for shardName {} was found so returning a noop registration",
97             shardName);
98
99         return new NoOpDataChangeListenerRegistration(listener);
100     }
101
102     @Override
103     public DOMStoreTransactionChain createTransactionChain() {
104         return new TransactionChainProxy(actorContext);
105     }
106
107     @Override
108     public DOMStoreReadTransaction newReadOnlyTransaction() {
109         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
110     }
111
112     @Override
113     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
114         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
115     }
116
117     @Override
118     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
119         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
120     }
121
122     @Override
123     public void onGlobalContextUpdated(SchemaContext schemaContext) {
124         actorContext.setSchemaContext(schemaContext);
125     }
126
127     @Override
128     public void close() throws Exception {
129         actorContext.shutdown();
130     }
131 }