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