Merge "Install snapshot and Reply"
[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 java.util.concurrent.TimeUnit;
12
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSystem;
15
16 import com.google.common.base.Preconditions;
17
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.md.sal.dom.store.impl.InMemoryDOMDataStoreConfigProperties;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import scala.concurrent.duration.Duration;
40
41 /**
42  *
43  */
44 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
45
46     private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
47
48     private final ActorContext actorContext;
49     private final DatastoreContext datastoreContext;
50
51     public DistributedDataStore(ActorSystem actorSystem, String type, ClusterWrapper cluster,
52             Configuration configuration, DistributedDataStoreProperties dataStoreProperties) {
53         Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
54         Preconditions.checkNotNull(type, "type should not be null");
55         Preconditions.checkNotNull(cluster, "cluster should not be null");
56         Preconditions.checkNotNull(configuration, "configuration should not be null");
57
58
59         String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
60
61         LOG.info("Creating ShardManager : {}", shardManagerId);
62
63         datastoreContext = new DatastoreContext(InMemoryDOMDataStoreConfigProperties.create(
64                 dataStoreProperties.getMaxShardDataChangeExecutorPoolSize(),
65                 dataStoreProperties.getMaxShardDataChangeExecutorQueueSize(),
66                 dataStoreProperties.getMaxShardDataChangeListenerQueueSize()),
67                 Duration.create(dataStoreProperties.getShardTransactionIdleTimeoutInMinutes(),
68                         TimeUnit.MINUTES));
69
70         actorContext
71                 = new ActorContext(
72                     actorSystem, actorSystem.actorOf(
73                         ShardManager.props(type, cluster, configuration, datastoreContext).
74                             withMailbox(ActorContext.MAILBOX), shardManagerId ), cluster, configuration);
75
76         actorContext.setOperationTimeout(dataStoreProperties.getOperationTimeoutInSeconds());
77     }
78
79     public DistributedDataStore(ActorContext actorContext) {
80         this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
81         this.datastoreContext = new DatastoreContext();
82     }
83
84
85     @SuppressWarnings("unchecked")
86     @Override
87     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
88                                               ListenerRegistration<L> registerChangeListener(
89         YangInstanceIdentifier path, L listener,
90         AsyncDataBroker.DataChangeScope scope) {
91
92         Preconditions.checkNotNull(path, "path should not be null");
93         Preconditions.checkNotNull(listener, "listener should not be null");
94
95         LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
96
97         ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
98             DataChangeListener.props(listener ));
99
100         String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
101
102         Object result = actorContext.executeLocalShardOperation(shardName,
103             new RegisterChangeListener(path, dataChangeListenerActor.path(), scope));
104
105         if (result != null) {
106             RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
107             return new DataChangeListenerRegistrationProxy(actorContext
108                 .actorSelection(reply.getListenerRegistrationPath()), listener,
109                 dataChangeListenerActor);
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 }