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 java.util.concurrent.TimeUnit;
13 import akka.actor.ActorRef;
14 import akka.actor.ActorSystem;
16 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.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;
39 import scala.concurrent.duration.Duration;
44 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
46 private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
48 private final ActorContext actorContext;
49 private final DatastoreContext datastoreContext;
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");
59 String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
61 LOG.info("Creating ShardManager : {}", shardManagerId);
63 datastoreContext = new DatastoreContext(InMemoryDOMDataStoreConfigProperties.create(
64 dataStoreProperties.getMaxShardDataChangeExecutorPoolSize(),
65 dataStoreProperties.getMaxShardDataChangeExecutorQueueSize(),
66 dataStoreProperties.getMaxShardDataChangeListenerQueueSize()),
67 Duration.create(dataStoreProperties.getShardTransactionIdleTimeoutInMinutes(),
72 actorSystem, actorSystem.actorOf(
73 ShardManager.props(type, cluster, configuration, datastoreContext).
74 withMailbox(ActorContext.MAILBOX), shardManagerId ), cluster, configuration);
76 actorContext.setOperationTimeout(dataStoreProperties.getOperationTimeoutInSeconds());
79 public DistributedDataStore(ActorContext actorContext) {
80 this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
81 this.datastoreContext = new DatastoreContext();
85 @SuppressWarnings("unchecked")
87 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
88 ListenerRegistration<L> registerChangeListener(
89 YangInstanceIdentifier path, L listener,
90 AsyncDataBroker.DataChangeScope scope) {
92 Preconditions.checkNotNull(path, "path should not be null");
93 Preconditions.checkNotNull(listener, "listener should not be null");
95 LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
97 ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
98 DataChangeListener.props(listener ));
100 String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
102 Object result = actorContext.executeLocalShardOperation(shardName,
103 new RegisterChangeListener(path, dataChangeListenerActor.path(), scope));
105 if (result != null) {
106 RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
107 return new DataChangeListenerRegistrationProxy(actorContext
108 .actorSelection(reply.getListenerRegistrationPath()), listener,
109 dataChangeListenerActor);
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();