Merge "Implement DistributedDataStore#registerDataChangeListener"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DistributedDataStore.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
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;
38
39 import java.util.concurrent.TimeUnit;
40
41 import static akka.pattern.Patterns.ask;
42
43 /**
44  *
45  */
46 public class DistributedDataStore implements DOMStore, SchemaContextListener {
47
48     private static final Logger
49         LOG = LoggerFactory.getLogger(DistributedDataStore.class);
50
51     final FiniteDuration ASK_DURATION = Duration.create(5, TimeUnit.SECONDS);
52     final Duration AWAIT_DURATION = Duration.create(5, TimeUnit.SECONDS);
53
54     private final ActorRef shardManager;
55     private final ActorSystem actorSystem;
56     private final String type;
57
58
59     public DistributedDataStore(ActorSystem actorSystem, String type) {
60         this.actorSystem = actorSystem;
61         this.type = type;
62         shardManager = actorSystem.actorOf(ShardManager.props(type));
63     }
64
65     @Override
66     public <L extends AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
67         InstanceIdentifier path, L listener,
68         AsyncDataBroker.DataChangeScope scope) {
69
70         ActorSelection primary = findPrimary();
71
72         ActorRef dataChangeListenerActor = actorSystem.actorOf(DataChangeListener.props());
73
74         Object result =
75             getResult(primary, new RegisterChangeListener(path, dataChangeListenerActor.path(),
76                 AsyncDataBroker.DataChangeScope.BASE), ASK_DURATION);
77
78         RegisterChangeListenerReply reply = (RegisterChangeListenerReply) result;
79         return new ListenerRegistrationProxy(reply.getListenerRegistrationPath());
80     }
81
82     private ActorSelection findPrimary() {
83         Object result = getResult(shardManager, new FindPrimary(Shard.DEFAULT_NAME), ASK_DURATION);
84
85         if(result instanceof PrimaryFound){
86             PrimaryFound found = (PrimaryFound) result;
87             LOG.error("Primary found {}", found.getPrimaryPath());
88
89             return actorSystem.actorSelection(found.getPrimaryPath());
90         }
91         throw new RuntimeException("primary was not found");
92     }
93
94     private Object getResult(ActorRef actor, Object message, FiniteDuration duration){
95         Future<Object> future =
96             ask(actor, message, new Timeout(duration));
97
98         try {
99             return Await.result(future, AWAIT_DURATION);
100         } catch (Exception e) {
101             throw new RuntimeException(e);
102         }
103     }
104
105     private Object getResult(ActorSelection actor, Object message, FiniteDuration duration){
106         Future<Object> future =
107             ask(actor, message, new Timeout(duration));
108
109         try {
110             return Await.result(future, AWAIT_DURATION);
111         } catch (Exception e) {
112             throw new RuntimeException(e);
113         }
114     }
115
116
117     @Override
118     public DOMStoreTransactionChain createTransactionChain() {
119         return new TransactionChainProxy();
120     }
121
122     @Override
123     public DOMStoreReadTransaction newReadOnlyTransaction() {
124         return new TransactionProxy();
125     }
126
127     @Override
128     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
129         return new TransactionProxy();
130     }
131
132     @Override
133     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
134         return new TransactionProxy();
135     }
136
137     @Override public void onGlobalContextUpdated(SchemaContext schemaContext) {
138         shardManager.tell(new UpdateSchemaContext(schemaContext), null);
139     }
140 }