cab35dd7af365685f94b78b01b2e4251e5df9adc
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / Shard.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.event.Logging;
13 import akka.event.LoggingAdapter;
14 import akka.persistence.UntypedProcessor;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
18 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
19 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
20 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
21 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
23
24 import java.util.concurrent.Executors;
25
26 /**
27  * A Shard represents a portion of the logical data tree
28  * <p/>
29  * Our Shard uses InMemoryDataStore as it's internal representation and delegates all requests it
30  */
31 public class Shard extends UntypedProcessor {
32
33   ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
34
35   private final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor);
36
37   LoggingAdapter log = Logging.getLogger(getContext().system(), this);
38
39   @Override
40   public void onReceive(Object message) throws Exception {
41     if (message instanceof CreateTransactionChain) {
42       createTransactionChain();
43     } else if(message instanceof RegisterChangeListener){
44       registerChangeListener((RegisterChangeListener) message);
45     }
46   }
47
48   private void registerChangeListener(RegisterChangeListener registerChangeListener) {
49 //    org.opendaylight.yangtools.concepts.ListenerRegistration<AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> registration =
50 //            store.registerChangeListener(registerChangeListener.getPath(), registerChangeListener.getListener(), registerChangeListener.getScope());
51     // TODO: Construct a ListenerRegistration actor with the actual registration returned when registering a listener with the datastore
52     ActorRef listenerRegistration = getContext().actorOf(ListenerRegistration.props(null));
53     getSender().tell(new RegisterChangeListenerReply(listenerRegistration.path()), getSelf());
54   }
55
56   private void createTransactionChain() {
57     DOMStoreTransactionChain chain = store.createTransactionChain();
58     ActorRef transactionChain = getContext().actorOf(TransactionChain.props(chain));
59     getSender().tell(new CreateTransactionChainReply(transactionChain.path()), getSelf());
60   }
61 }