Merge "Include JMX Counters and resetTransactionCounters"
[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 java.util.concurrent.TimeUnit;
12
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSystem;
15
16 import com.google.common.base.Preconditions;
17
18 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
19 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
20 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
21 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
22 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
24 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
25 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreConfigProperties;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import scala.concurrent.duration.Duration;
40
41 /**
42  *
43  */
44 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
45
46     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
47
48     private final ActorContext actorContext;
49     private final DatastoreContext datastoreContext;
50
51     public DistributedDataStore(ActorSystem actorSystem, String type, ClusterWrapper cluster,
52             Configuration configuration, DistributedDataStoreProperties dataStoreProperties) {
53         Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
54         Preconditions.checkNotNull(type, "type should not be null");
55         Preconditions.checkNotNull(cluster, "cluster should not be null");
56         Preconditions.checkNotNull(configuration, "configuration should not be null");
57
58
59         String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
60
61         LOG.info("Creating ShardManager : {}", shardManagerId);
62
63         datastoreContext = new DatastoreContext(InMemoryDOMDataStoreConfigProperties.create(
64                 dataStoreProperties.getMaxShardDataChangeExecutorPoolSize(),
65                 dataStoreProperties.getMaxShardDataChangeExecutorQueueSize(),
66                 dataStoreProperties.getMaxShardDataChangeListenerQueueSize()),
67                 Duration.create(dataStoreProperties.getShardTransactionIdleTimeoutInMinutes(),
68                         TimeUnit.MINUTES));
69
70         actorContext
71                 = new ActorContext(
72                     actorSystem, actorSystem.actorOf(
73                         ShardManager.props(type, cluster, configuration, datastoreContext).
74                             withMailbox(ActorContext.MAILBOX), shardManagerId ), cluster, configuration);
75     }
76
77     public DistributedDataStore(ActorContext actorContext) {
78         this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
79         this.datastoreContext = new DatastoreContext();
80     }
81
82
83     @SuppressWarnings("unchecked")
84     @Override
85     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
86                                               ListenerRegistration<L> registerChangeListener(
87         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         ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
96             DataChangeListener.props(listener ));
97
98         String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
99
100         Object result = actorContext.executeLocalShardOperation(shardName,
101             new RegisterChangeListener(path, dataChangeListenerActor.path(), scope),
102             ActorContext.ASK_DURATION);
103
104         if (result != null) {
105             RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
106             return new DataChangeListenerRegistrationProxy(actorContext
107                 .actorSelection(reply.getListenerRegistrationPath()), listener,
108                 dataChangeListenerActor);
109         }
110
111         LOG.debug(
112             "No local shard for shardName {} was found so returning a noop registration",
113             shardName);
114
115         return new NoOpDataChangeListenerRegistration(listener);
116     }
117
118     @Override
119     public DOMStoreTransactionChain createTransactionChain() {
120         return new TransactionChainProxy(actorContext);
121     }
122
123     @Override
124     public DOMStoreReadTransaction newReadOnlyTransaction() {
125         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
126     }
127
128     @Override
129     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
130         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
131     }
132
133     @Override
134     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
135         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
136     }
137
138     @Override
139     public void onGlobalContextUpdated(SchemaContext schemaContext) {
140         actorContext.setSchemaContext(schemaContext);
141     }
142
143     @Override
144     public void close() throws Exception {
145         actorContext.shutdown();
146     }
147 }