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.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;
41 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
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
46 private final ActorContext actorContext;
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");
56 String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
58 LOG.info("Creating ShardManager : {}", shardManagerId);
60 actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
61 ShardManager.props(type, cluster, configuration, datastoreContext)
62 .withMailbox(ActorContext.MAILBOX), shardManagerId ), cluster, configuration);
64 actorContext.setOperationTimeout(datastoreContext.getOperationTimeoutInSeconds());
67 public DistributedDataStore(ActorContext actorContext) {
68 this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
71 @SuppressWarnings("unchecked")
73 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
74 ListenerRegistration<L> registerChangeListener(
75 final YangInstanceIdentifier path, L listener,
76 AsyncDataBroker.DataChangeScope scope) {
78 Preconditions.checkNotNull(path, "path should not be null");
79 Preconditions.checkNotNull(listener, "listener should not be null");
81 LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
83 String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
85 Optional<ActorRef> shard = actorContext.findLocalShard(shardName);
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);
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)));
98 final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
99 new DataChangeListenerRegistrationProxy(listener, dataChangeListenerActor);
101 future.onComplete(new OnComplete() {
104 public void onComplete(Throwable failure, Object result)
106 if (failure != null) {
107 LOG.error("Failed to register listener at path " + path.toString(), failure);
110 RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
111 listenerRegistrationProxy.setListenerRegistrationActor(actorContext
112 .actorSelection(reply.getListenerRegistrationPath()));
114 }, actorContext.getActorSystem().dispatcher());
116 return listenerRegistrationProxy;
121 public DOMStoreTransactionChain createTransactionChain() {
122 return new TransactionChainProxy(actorContext);
126 public DOMStoreReadTransaction newReadOnlyTransaction() {
127 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
131 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
132 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
136 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
137 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
141 public void onGlobalContextUpdated(SchemaContext schemaContext) {
142 actorContext.setSchemaContext(schemaContext);
146 public void close() throws Exception {
147 actorContext.shutdown();