Merge "BUG-190 Simplify reconnect logic in protocol-framework."
[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.YangInstanceIdentifier;
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
65             .actorOf(ShardManager.props(type, cluster, configuration),
66                 "shardmanager-" + type), cluster, configuration), type);
67     }
68
69     public DistributedDataStore(ActorContext actorContext, String type) {
70         this.type = type;
71         this.actorContext = actorContext;
72     }
73
74
75     @Override
76     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
77         YangInstanceIdentifier path, L listener,
78         AsyncDataBroker.DataChangeScope scope) {
79
80         ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
81             DataChangeListener.props(schemaContext,listener,path ));
82
83         String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
84
85         Object result = actorContext.executeShardOperation(shardName,
86             new RegisterChangeListener(path, dataChangeListenerActor.path(),
87                 scope).toSerializable(),
88             ActorContext.ASK_DURATION
89         );
90
91         RegisterChangeListenerReply reply = RegisterChangeListenerReply.fromSerializable(actorContext.getActorSystem(),result);
92         return new DataChangeListenerRegistrationProxy(actorContext.actorSelection(reply.getListenerRegistrationPath()), listener, dataChangeListenerActor);
93     }
94
95
96
97     @Override
98     public DOMStoreTransactionChain createTransactionChain() {
99         return new TransactionChainProxy(actorContext, executor, schemaContext);
100     }
101
102     @Override
103     public DOMStoreReadTransaction newReadOnlyTransaction() {
104         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY,
105             executor, schemaContext);
106     }
107
108     @Override
109     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
110         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY,
111             executor, schemaContext);
112     }
113
114     @Override
115     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
116         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE,
117             executor, schemaContext);
118     }
119
120     @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
121         this.schemaContext = schemaContext;
122         actorContext.getShardManager().tell(
123             new UpdateSchemaContext(schemaContext), null);
124     }
125
126     @Override public void close() throws Exception {
127         actorContext.shutdown();
128
129     }
130 }