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 org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
14 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
15 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
16 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.Executors;
38 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
40 private static final Logger
41 LOG = LoggerFactory.getLogger(DistributedDataStore.class);
44 private final String type;
45 private final ActorContext actorContext;
49 * Executor used to run FutureTask's
51 * This is typically used when we need to make a request to an actor and
52 * wait for it's response and the consumer needs to be provided a Future.
54 * FIXME : Make the thread pool configurable
56 private final ExecutorService executor =
57 Executors.newFixedThreadPool(10);
59 public DistributedDataStore(ActorSystem actorSystem, String type) {
60 this(new ActorContext(actorSystem, actorSystem.actorOf(ShardManager.props(type), "shardmanager-" + type)), type);
63 public DistributedDataStore(ActorContext actorContext, String type) {
65 this.actorContext = actorContext;
70 public <L extends AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
71 InstanceIdentifier path, L listener,
72 AsyncDataBroker.DataChangeScope scope) {
74 ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
75 DataChangeListener.props(listener));
77 Object result = actorContext.executeShardOperation(Shard.DEFAULT_NAME,
78 new RegisterChangeListener(path, dataChangeListenerActor.path(),
79 AsyncDataBroker.DataChangeScope.BASE),
80 ActorContext.ASK_DURATION
83 RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
84 return new DataChangeListenerRegistrationProxy(actorContext.actorSelection(reply.getListenerRegistrationPath()), listener, dataChangeListenerActor);
90 public DOMStoreTransactionChain createTransactionChain() {
91 return new TransactionChainProxy(actorContext, executor);
95 public DOMStoreReadTransaction newReadOnlyTransaction() {
96 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY,
101 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
102 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY,
107 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
108 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE,
112 @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
113 actorContext.getShardManager().tell(
114 new UpdateSchemaContext(schemaContext), null);
117 @Override public void close() throws Exception {
118 actorContext.shutdown();