Merge "BUG-868: migrate ListenerRegistry"
[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 import com.google.common.util.concurrent.ListeningExecutorService;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
16 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
17 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
18 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
19 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import java.util.concurrent.Executors;
36
37 /**
38  *
39  */
40 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
41
42     private static final Logger
43         LOG = LoggerFactory.getLogger(DistributedDataStore.class);
44
45     private static final int DEFAULT_EXECUTOR_POOL_SIZE = 10;
46
47     private final String type;
48     private final ActorContext actorContext;
49
50     private SchemaContext schemaContext;
51
52
53
54     /**
55      * Executor used to run FutureTask's
56      *
57      * This is typically used when we need to make a request to an actor and
58      * wait for it's response and the consumer needs to be provided a Future.
59      *
60      * FIXME : Make the thread pool size configurable.
61      */
62     private final ListeningExecutorService executor =
63         MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(DEFAULT_EXECUTOR_POOL_SIZE));
64
65     public DistributedDataStore(ActorSystem actorSystem, String type, ClusterWrapper cluster, Configuration configuration) {
66         this(new ActorContext(actorSystem, actorSystem
67             .actorOf(ShardManager.props(type, cluster, configuration),
68                 "shardmanager-" + type), cluster, configuration), type);
69     }
70
71     public DistributedDataStore(ActorContext actorContext, String type) {
72         this.type = type;
73         this.actorContext = actorContext;
74     }
75
76
77     @Override
78     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
79         YangInstanceIdentifier path, L listener,
80         AsyncDataBroker.DataChangeScope scope) {
81
82         ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
83             DataChangeListener.props(schemaContext,listener,path ));
84
85         String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
86
87         Object result = actorContext.executeLocalShardOperation(shardName,
88             new RegisterChangeListener(path, dataChangeListenerActor.path(),
89                 scope),
90             ActorContext.ASK_DURATION
91         );
92
93         if (result != null) {
94             RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
95             return new DataChangeListenerRegistrationProxy(actorContext
96                 .actorSelection(reply.getListenerRegistrationPath()), listener,
97                 dataChangeListenerActor);
98         }
99
100         LOG.debug(
101             "No local shard for shardName {} was found so returning a noop registration",
102             shardName);
103         return new NoOpDataChangeListenerRegistration(listener);
104     }
105
106
107
108
109
110     @Override
111     public DOMStoreTransactionChain createTransactionChain() {
112         return new TransactionChainProxy(actorContext, executor, schemaContext);
113     }
114
115     @Override
116     public DOMStoreReadTransaction newReadOnlyTransaction() {
117         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY,
118             executor, schemaContext);
119     }
120
121     @Override
122     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
123         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY,
124             executor, schemaContext);
125     }
126
127     @Override
128     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
129         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE,
130             executor, schemaContext);
131     }
132
133     @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
134         this.schemaContext = schemaContext;
135         actorContext.getShardManager().tell(
136             new UpdateSchemaContext(schemaContext), null);
137     }
138
139     @Override public void close() throws Exception {
140         actorContext.shutdown();
141
142     }
143 }