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;
14 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;
39 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
41 private static final Logger LOG = LoggerFactory.getLogger(DistributedDataStore.class);
43 private final ActorContext actorContext;
45 public DistributedDataStore(ActorSystem actorSystem, String type, ClusterWrapper cluster,
46 Configuration configuration, DatastoreContext datastoreContext) {
47 Preconditions.checkNotNull(actorSystem, "actorSystem should not be null");
48 Preconditions.checkNotNull(type, "type should not be null");
49 Preconditions.checkNotNull(cluster, "cluster should not be null");
50 Preconditions.checkNotNull(configuration, "configuration should not be null");
51 Preconditions.checkNotNull(datastoreContext, "datastoreContext should not be null");
53 String shardManagerId = ShardManagerIdentifier.builder().type(type).build().toString();
55 LOG.info("Creating ShardManager : {}", shardManagerId);
57 actorContext = new ActorContext(actorSystem, actorSystem.actorOf(
58 ShardManager.props(type, cluster, configuration, datastoreContext)
59 .withMailbox(ActorContext.MAILBOX), shardManagerId ), cluster, configuration);
61 actorContext.setOperationTimeout(datastoreContext.getOperationTimeoutInSeconds());
64 public DistributedDataStore(ActorContext actorContext) {
65 this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
68 @SuppressWarnings("unchecked")
70 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
71 ListenerRegistration<L> registerChangeListener(
72 YangInstanceIdentifier path, L listener,
73 AsyncDataBroker.DataChangeScope scope) {
75 Preconditions.checkNotNull(path, "path should not be null");
76 Preconditions.checkNotNull(listener, "listener should not be null");
78 LOG.debug("Registering listener: {} for path: {} scope: {}", listener, path, scope);
80 ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
81 DataChangeListener.props(listener ));
83 String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
85 Object result = actorContext.executeLocalShardOperation(shardName,
86 new RegisterChangeListener(path, dataChangeListenerActor.path(), scope));
89 RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
90 return new DataChangeListenerRegistrationProxy(actorContext
91 .actorSelection(reply.getListenerRegistrationPath()), listener,
92 dataChangeListenerActor);
96 "No local shard for shardName {} was found so returning a noop registration",
99 return new NoOpDataChangeListenerRegistration(listener);
103 public DOMStoreTransactionChain createTransactionChain() {
104 return new TransactionChainProxy(actorContext);
108 public DOMStoreReadTransaction newReadOnlyTransaction() {
109 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY);
113 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
114 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY);
118 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
119 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE);
123 public void onGlobalContextUpdated(SchemaContext schemaContext) {
124 actorContext.setSchemaContext(schemaContext);
128 public void close() throws Exception {
129 actorContext.shutdown();