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