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;
47 private SchemaContext schemaContext;
52 * Executor used to run FutureTask's
54 * This is typically used when we need to make a request to an actor and
55 * wait for it's response and the consumer needs to be provided a Future.
57 * FIXME : Make the thread pool configurable
59 private final ExecutorService executor =
60 Executors.newFixedThreadPool(10);
62 public DistributedDataStore(ActorSystem actorSystem, String type) {
63 this(new ActorContext(actorSystem, actorSystem.actorOf(ShardManager.props(type), "shardmanager-" + type)), type);
66 public DistributedDataStore(ActorContext actorContext, String type) {
68 this.actorContext = actorContext;
73 public <L extends AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
74 InstanceIdentifier path, L listener,
75 AsyncDataBroker.DataChangeScope scope) {
77 ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
78 DataChangeListener.props(listener));
80 Object result = actorContext.executeShardOperation(Shard.DEFAULT_NAME,
81 new RegisterChangeListener(path, dataChangeListenerActor.path(),
82 AsyncDataBroker.DataChangeScope.BASE),
83 ActorContext.ASK_DURATION
86 RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
87 return new DataChangeListenerRegistrationProxy(actorContext.actorSelection(reply.getListenerRegistrationPath()), listener, dataChangeListenerActor);
93 public DOMStoreTransactionChain createTransactionChain() {
94 return new TransactionChainProxy(actorContext, executor, schemaContext);
98 public DOMStoreReadTransaction newReadOnlyTransaction() {
99 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY,
100 executor, schemaContext);
104 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
105 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY,
106 executor, schemaContext);
110 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
111 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE,
112 executor, schemaContext);
115 @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
116 this.schemaContext = schemaContext;
117 actorContext.getShardManager().tell(
118 new UpdateSchemaContext(schemaContext), null);
121 @Override public void close() throws Exception {
122 actorContext.shutdown();