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