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