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