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.ActorSelection;
13 import akka.actor.ActorSystem;
14 import akka.util.Timeout;
15 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
16 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
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.messages.UpdateSchemaContext;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import scala.concurrent.Await;
35 import scala.concurrent.Future;
36 import scala.concurrent.duration.Duration;
37 import scala.concurrent.duration.FiniteDuration;
39 import java.util.concurrent.TimeUnit;
41 import static akka.pattern.Patterns.ask;
46 public class DistributedDataStore implements DOMStore, SchemaContextListener {
48 private static final Logger
49 LOG = LoggerFactory.getLogger(DistributedDataStore.class);
51 final FiniteDuration ASK_DURATION = Duration.create(5, TimeUnit.SECONDS);
52 final Duration AWAIT_DURATION = Duration.create(5, TimeUnit.SECONDS);
54 private final ActorRef shardManager;
55 private final ActorSystem actorSystem;
56 private final String type;
59 public DistributedDataStore(ActorSystem actorSystem, String type) {
60 this.actorSystem = actorSystem;
62 shardManager = actorSystem.actorOf(ShardManager.props(type));
66 public <L extends AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
67 InstanceIdentifier path, L listener,
68 AsyncDataBroker.DataChangeScope scope) {
70 ActorSelection primary = findPrimary();
72 ActorRef dataChangeListenerActor = actorSystem.actorOf(DataChangeListener.props());
75 getResult(primary, new RegisterChangeListener(path, dataChangeListenerActor.path(),
76 AsyncDataBroker.DataChangeScope.BASE), ASK_DURATION);
78 RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
79 return new ListenerRegistrationProxy(reply.getListenerRegistrationPath());
82 private ActorSelection findPrimary() {
83 Object result = getResult(shardManager, new FindPrimary(Shard.DEFAULT_NAME), ASK_DURATION);
85 if(result instanceof PrimaryFound){
86 PrimaryFound found = (PrimaryFound) result;
87 LOG.error("Primary found {}", found.getPrimaryPath());
89 return actorSystem.actorSelection(found.getPrimaryPath());
91 throw new RuntimeException("primary was not found");
94 private Object getResult(ActorRef actor, Object message, FiniteDuration duration){
95 Future<Object> future =
96 ask(actor, message, new Timeout(duration));
99 return Await.result(future, AWAIT_DURATION);
100 } catch (Exception e) {
101 throw new RuntimeException(e);
105 private Object getResult(ActorSelection actor, Object message, FiniteDuration duration){
106 Future<Object> future =
107 ask(actor, message, new Timeout(duration));
110 return Await.result(future, AWAIT_DURATION);
111 } catch (Exception e) {
112 throw new RuntimeException(e);
118 public DOMStoreTransactionChain createTransactionChain() {
119 return new TransactionChainProxy();
123 public DOMStoreReadTransaction newReadOnlyTransaction() {
124 return new TransactionProxy();
128 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
129 return new TransactionProxy();
133 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
134 return new TransactionProxy();
137 @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
138 shardManager.tell(new UpdateSchemaContext(schemaContext), null);