Merge "Added comments to the opendaylight-inventory.yang file to help describe the...
[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
48     /**
49      * Executor used to run FutureTask's
50      *
51      * This is typically used when we need to make a request to an actor and
52      * wait for it's response and the consumer needs to be provided a Future.
53      *
54      * FIXME : Make the thread pool configurable
55      */
56     private final ExecutorService executor =
57         Executors.newFixedThreadPool(10);
58
59     public DistributedDataStore(ActorSystem actorSystem, String type) {
60         this(new ActorContext(actorSystem, actorSystem.actorOf(ShardManager.props(type), "shardmanager-" + type)), type);
61     }
62
63     public DistributedDataStore(ActorContext actorContext, String type) {
64         this.type = type;
65         this.actorContext = actorContext;
66     }
67
68
69     @Override
70     public <L extends AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
71         InstanceIdentifier path, L listener,
72         AsyncDataBroker.DataChangeScope scope) {
73
74         ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
75             DataChangeListener.props(listener));
76
77         Object result = actorContext.executeShardOperation(Shard.DEFAULT_NAME,
78             new RegisterChangeListener(path, dataChangeListenerActor.path(),
79                 AsyncDataBroker.DataChangeScope.BASE),
80             ActorContext.ASK_DURATION
81         );
82
83         RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
84         return new DataChangeListenerRegistrationProxy(actorContext.actorSelection(reply.getListenerRegistrationPath()), listener, dataChangeListenerActor);
85     }
86
87
88
89     @Override
90     public DOMStoreTransactionChain createTransactionChain() {
91         return new TransactionChainProxy(actorContext, executor);
92     }
93
94     @Override
95     public DOMStoreReadTransaction newReadOnlyTransaction() {
96         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY,
97             executor);
98     }
99
100     @Override
101     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
102         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY,
103             executor);
104     }
105
106     @Override
107     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
108         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE,
109             executor);
110     }
111
112     @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
113         actorContext.getShardManager().tell(
114             new UpdateSchemaContext(schemaContext), null);
115     }
116
117     @Override public void close() throws Exception {
118         actorContext.shutdown();
119
120     }
121 }