bf541d95deadeb3e9ecce59c83cdb988934289a5
[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.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;
36
37 /**
38  *
39  */
40 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
41
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
44
45     private final ActorContext actorContext;
46
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");
54
55         String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
56
57         LOG.info("Creating ShardManager : {}", shardManagerId);
58
59         actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
60                 ShardManager.props(type, cluster, configuration, datastoreContext)
61                     .withMailbox(ActorContext.MAILBOX), shardManagerId ), cluster, configuration);
62
63         actorContext.setOperationTimeout(datastoreContext.getOperationTimeoutInSeconds());
64     }
65
66     public DistributedDataStore(ActorContext actorContext) {
67         this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
68     }
69
70     @SuppressWarnings("unchecked")
71     @Override
72     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
73                                               ListenerRegistration<L> registerChangeListener(
74         final YangInstanceIdentifier path, L listener,
75         AsyncDataBroker.DataChangeScope scope) {
76
77         Preconditions.checkNotNull(path, "path should not be null");
78         Preconditions.checkNotNull(listener, "listener should not be null");
79
80         LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
81
82         ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
83             DataChangeListener.props(listener ));
84
85         String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
86
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)));
91
92         if (future != null) {
93             final DataChangeListenerRegistrationProxy listenerRegistrationProxy =
94                 new DataChangeListenerRegistrationProxy(listener, dataChangeListenerActor);
95
96             future.onComplete(new OnComplete(){
97
98                 @Override public void onComplete(Throwable failure, Object result)
99                     throws Throwable {
100                     if(failure != null){
101                         LOG.error("Failed to register listener at path " + path.toString(), failure);
102                         return;
103                     }
104                     RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
105                     listenerRegistrationProxy.setListenerRegistrationActor(actorContext
106                         .actorSelection(reply.getListenerRegistrationPath()));
107                 }
108             }, actorContext.getActorSystem().dispatcher());
109             return listenerRegistrationProxy;
110         }
111
112         LOG.debug(
113             "No local shard for shardName {} was found so returning a noop registration",
114             shardName);
115
116         return new NoOpDataChangeListenerRegistration(listener);
117     }
118
119     @Override
120     public DOMStoreTransactionChain createTransactionChain() {
121         return new TransactionChainProxy(actorContext);
122     }
123
124     @Override
125     public DOMStoreReadTransaction newReadOnlyTransaction() {
126         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
127     }
128
129     @Override
130     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
131         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
132     }
133
134     @Override
135     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
136         return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
137     }
138
139     @Override
140     public void onGlobalContextUpdated(SchemaContext schemaContext) {
141         actorContext.setSchemaContext(schemaContext);
142     }
143
144     @Override
145     public void close() throws Exception {
146         actorContext.shutdown();
147     }
148 }