2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore;
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;
42 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
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
47 private final ActorContext actorContext;
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");
57 String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
59 LOG.info("Creating ShardManager : {}", shardManagerId);
61 actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
62 ShardManager.props(type, cluster, configuration, datastoreContext)
63 .withMailbox(ActorContext.MAILBOX), shardManagerId ), cluster, configuration);
65 actorContext.setOperationTimeout(datastoreContext.getOperationTimeoutInSeconds());
68 public DistributedDataStore(ActorContext actorContext) {
69 this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
72 @SuppressWarnings("unchecked")
74 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
75 ListenerRegistration<L> registerChangeListener(
76 final YangInstanceIdentifier path, L listener,
77 AsyncDataBroker.DataChangeScope scope) {
79 Preconditions.checkNotNull(path, "path should not be null");
80 Preconditions.checkNotNull(listener, "listener should not be null");
82 LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
84 String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
86 Optional<ActorRef> shard = actorContext.findLocalShard(shardName);
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);
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)));
99 final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
100 new DataChangeListenerRegistrationProxy(listener, dataChangeListenerActor);
102 future.onComplete(new OnComplete() {
105 public void onComplete(Throwable failure, Object result)
107 if (failure != null) {
108 LOG.error("Failed to register listener at path " + path.toString(), failure);
111 RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
112 listenerRegistrationProxy.setListenerRegistrationActor(actorContext
113 .actorSelection(reply.getListenerRegistrationPath()));
115 }, actorContext.getActorSystem().dispatcher());
117 return listenerRegistrationProxy;
122 public DOMStoreTransactionChain createTransactionChain() {
123 return new TransactionChainProxy(actorContext);
127 public DOMStoreReadTransaction newReadOnlyTransaction() {
128 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
132 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
133 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
137 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
138 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
142 public void onGlobalContextUpdated(SchemaContext schemaContext) {
143 actorContext.setSchemaContext(schemaContext);
147 public void close() throws Exception {
148 actorContext.shutdown();
152 ActorContext getActorContext() {