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.shardstrategy.ShardStrategyFactory;
17 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import java.util.concurrent.ExecutorService;
34 import java.util.concurrent.Executors;
39 public class DistributedDataStore implements DOMStore, SchemaContextListener, AutoCloseable {
41 private static final Logger
42 LOG = LoggerFactory.getLogger(DistributedDataStore.class);
45 private final String type;
46 private final ActorContext actorContext;
48 private SchemaContext schemaContext;
53 * Executor used to run FutureTask's
55 * This is typically used when we need to make a request to an actor and
56 * wait for it's response and the consumer needs to be provided a Future.
58 * FIXME : Make the thread pool configurable
60 private final ExecutorService executor =
61 Executors.newFixedThreadPool(10);
63 public DistributedDataStore(ActorSystem actorSystem, String type, ClusterWrapper cluster, Configuration configuration) {
64 this(new ActorContext(actorSystem, actorSystem
65 .actorOf(ShardManager.props(type, cluster, configuration),
66 "shardmanager-" + type), cluster, configuration), type);
69 public DistributedDataStore(ActorContext actorContext, String type) {
71 this.actorContext = actorContext;
76 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
77 YangInstanceIdentifier path, L listener,
78 AsyncDataBroker.DataChangeScope scope) {
80 ActorRef dataChangeListenerActor = actorContext.getActorSystem().actorOf(
81 DataChangeListener.props(schemaContext,listener,path ));
83 String shardName = ShardStrategyFactory.getStrategy(path).findShard(path);
85 Object result = actorContext.executeShardOperation(shardName,
86 new RegisterChangeListener(path, dataChangeListenerActor.path(),
87 scope).toSerializable(),
88 ActorContext.ASK_DURATION
91 RegisterChangeListenerReply reply = RegisterChangeListenerReply.fromSerializable(actorContext.getActorSystem(),result);
92 return new DataChangeListenerRegistrationProxy(actorContext.actorSelection(reply.getListenerRegistrationPath()), listener, dataChangeListenerActor);
98 public DOMStoreTransactionChain createTransactionChain() {
99 return new TransactionChainProxy(actorContext, executor, schemaContext);
103 public DOMStoreReadTransaction newReadOnlyTransaction() {
104 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_ONLY,
105 executor, schemaContext);
109 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
110 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.WRITE_ONLY,
111 executor, schemaContext);
115 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
116 return new TransactionProxy(actorContext, TransactionProxy.TransactionType.READ_WRITE,
117 executor, schemaContext);
120 @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
121 this.schemaContext = schemaContext;
122 actorContext.getShardManager().tell(
123 new UpdateSchemaContext(schemaContext), null);
126 @Override public void close() throws Exception {
127 actorContext.shutdown();