Merge "BUG-1958 Fix leafref path in config-yang module."
[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 akka.dispatch.OnComplete;
14 import akka.util.Timeout;
15 import com.google.common.base.Optional;
16 import com.google.common.base.Preconditions;
17 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
18 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
19 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
20 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
21 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
22 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import scala.concurrent.Future;
37
38 /**
39  *
40  */
41 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
42
43     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
44     public static final int REGISTER_DATA_CHANGE_LISTENER_TIMEOUT_FACTOR = 24; // 24 times the usual operation timeout
45
46     private final ActorContext actorContext;
47
48     public DistributedDataStore(ActorSystem actorSystem, String type, ClusterWrapper cluster,
49             Configuration configuration, DatastoreContext datastoreContext) {
50         Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
51         Preconditions.checkNotNull(type, "type should not be null");
52         Preconditions.checkNotNull(cluster, "cluster should not be null");
53         Preconditions.checkNotNull(configuration, "configuration should not be null");
54         Preconditions.checkNotNull(datastoreContext, "datastoreContext should not be null");
55
56         String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
57
58         LOG.info("Creating ShardManager : {}", shardManagerId);
59
60         actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
61                 ShardManager.props(type, cluster, configuration, datastoreContext)
62                     .withMailbox(ActorContext.MAILBOX), shardManagerId ), cluster, configuration);
63
64         actorContext.setOperationTimeout(datastoreContext.getOperationTimeoutInSeconds());
65     }
66
67     public DistributedDataStore(ActorContext actorContext) {
68         this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
69     }
70
71     @SuppressWarnings("unchecked")
72     @Override
73     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
74                                               ListenerRegistration<L> registerChangeListener(
75         final YangInstanceIdentifier path, L listener,
76         AsyncDataBroker.DataChangeScope scope) {
77
78         Preconditions.checkNotNull(path, "path should not be null");
79         Preconditions.checkNotNull(listener, "listener should not be null");
80
81         LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
82
83         String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
84
85         Optional<ActorRef> shard = actorContext.findLocalShard(shardName);
86
87         //if shard is NOT local
88         if (!shard.isPresent()) {
89             LOG.debug("No local shard for shardName {} was found so returning a noop registration", shardName);
90             return new NoOpDataChangeListenerRegistration(listener);
91         }
92         //if shard is local
93         ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(DataChangeListener.props(listener));
94         Future future = actorContext.executeOperationAsync(shard.get(),
95                 new RegisterChangeListener(path, dataChangeListenerActor.path(), scope),
96                 new Timeout(actorContext.getOperationDuration().$times(REGISTER_DATA_CHANGE_LISTENER_TIMEOUT_FACTOR)));
97
98         final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
99                 new DataChangeListenerRegistrationProxy(listener, dataChangeListenerActor);
100
101         future.onComplete(new OnComplete() {
102
103             @Override
104             public void onComplete(Throwable failure, Object result)
105                     throws Throwable {
106                 if (failure != null) {
107                     LOG.error("Failed to register listener at path " + path.toString(), failure);
108                     return;
109                 }
110                 RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
111                 listenerRegistrationProxy.setListenerRegistrationActor(actorContext
112                         .actorSelection(reply.getListenerRegistrationPath()));
113             }
114         }, actorContext.getActorSystem().dispatcher());
115
116         return listenerRegistrationProxy;
117
118     }
119
120     @Override
121     public DOMStoreTransactionChain createTransactionChain() {
122         return new TransactionChainProxy(actorContext);
123     }
124
125     @Override
126     public DOMStoreReadTransaction newReadOnlyTransaction() {
127         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
128     }
129
130     @Override
131     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
132         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
133     }
134
135     @Override
136     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
137         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
138     }
139
140     @Override
141     public void onGlobalContextUpdated(SchemaContext schemaContext) {
142         actorContext.setSchemaContext(schemaContext);
143     }
144
145     @Override
146     public void close() throws Exception {
147         actorContext.shutdown();
148     }
149 }