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.Preconditions;
16 import org.opendaylight.controller.cluster.datastore.identifiers.ShardManagerIdentifier;
17 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
18 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
19 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
20 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
22 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import scala.concurrent.Future;
40 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
42 private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
43 public static final int REGISTER_DATA_CHANGE_LISTENER_TIMEOUT_FACTOR = 24; // 24 times the usual operation timeout
45 private final ActorContext actorContext;
47 public DistributedDataStore(ActorSystem actorSystem, String type, ClusterWrapper cluster,
48 Configuration configuration, DatastoreContext datastoreContext) {
49 Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
50 Preconditions.checkNotNull(type, "type should not be null");
51 Preconditions.checkNotNull(cluster, "cluster should not be null");
52 Preconditions.checkNotNull(configuration, "configuration should not be null");
53 Preconditions.checkNotNull(datastoreContext, "datastoreContext should not be null");
55 String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
57 LOG.info("Creating ShardManager : {}", shardManagerId);
59 actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
60 ShardManager.props(type, cluster, configuration, datastoreContext)
61 .withMailbox(ActorContext.MAILBOX), shardManagerId ), cluster, configuration);
63 actorContext.setOperationTimeout(datastoreContext.getOperationTimeoutInSeconds());
66 public DistributedDataStore(ActorContext actorContext) {
67 this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
70 @SuppressWarnings("unchecked")
72 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
73 ListenerRegistration<L> registerChangeListener(
74 final YangInstanceIdentifier path, L listener,
75 AsyncDataBroker.DataChangeScope scope) {
77 Preconditions.checkNotNull(path, "path should not be null");
78 Preconditions.checkNotNull(listener, "listener should not be null");
79 if(LOG.isDebugEnabled()) {
80 LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
82 ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
83 DataChangeListener.props(listener ));
85 String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
87 Future future = actorContext.executeLocalShardOperationAsync(shardName,
88 new RegisterChangeListener(path, dataChangeListenerActor.path(), scope),
89 new Timeout(actorContext.getOperationDuration().$times(
90 REGISTER_DATA_CHANGE_LISTENER_TIMEOUT_FACTOR)));
93 final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
94 new DataChangeListenerRegistrationProxy(listener, dataChangeListenerActor);
96 future.onComplete(new OnComplete(){
98 @Override public void onComplete(Throwable failure, Object result)
101 LOG.error("Failed to register listener at path " + path.toString(), failure);
104 RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
105 listenerRegistrationProxy.setListenerRegistrationActor(actorContext
106 .actorSelection(reply.getListenerRegistrationPath()));
108 }, actorContext.getActorSystem().dispatcher());
109 return listenerRegistrationProxy;
111 if(LOG.isDebugEnabled()) {
113 "No local shard for shardName {} was found so returning a noop registration",
116 return new NoOpDataChangeListenerRegistration(listener);
120 public DOMStoreTransactionChain createTransactionChain() {
121 return new TransactionChainProxy(actorContext);
125 public DOMStoreReadTransaction newReadOnlyTransaction() {
126 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
130 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
131 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
135 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
136 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
140 public void onGlobalContextUpdated(SchemaContext schemaContext) {
141 actorContext.setSchemaContext(schemaContext);
145 public void close() throws Exception {
146 actorContext.shutdown();