Implement finding a primary based on the shard name and do basic wiring of Distribute...
[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.actor.Props;
13 import akka.event.Logging;
14 import akka.event.LoggingAdapter;
15 import akka.japi.Creator;
16 import akka.persistence.UntypedProcessor;
17 import com.google.common.util.concurrent.ListeningExecutorService;
18 import com.google.common.util.concurrent.MoreExecutors;
19 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
20 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
21 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
22 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
23 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
24 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
25 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
27 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29
30 import java.util.concurrent.Executors;
31
32 /**
33  * A Shard represents a portion of the logical data tree <br/>
34  * <p>
35  * Our Shard uses InMemoryDataStore as it's internal representation and delegates all requests it
36  * </p>
37  */
38 public class Shard extends UntypedProcessor {
39
40   public static final String DEFAULT_NAME = "default";
41
42   private final ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
43
44   private final InMemoryDOMDataStore store;
45
46   private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
47
48   private Shard(String name){
49     store = new InMemoryDOMDataStore(name, storeExecutor);
50   }
51
52   public static Props props(final String name) {
53     return Props.create(new Creator<Shard>() {
54
55       @Override
56       public Shard create() throws Exception {
57         return new Shard(name);
58       }
59
60     });
61   }
62
63   @Override
64   public void onReceive(Object message) throws Exception {
65     if (message instanceof CreateTransactionChain) {
66       createTransactionChain();
67     } else if (message instanceof RegisterChangeListener) {
68       registerChangeListener((RegisterChangeListener) message);
69     } else if (message instanceof UpdateSchemaContext) {
70       updateSchemaContext((UpdateSchemaContext) message);
71     }
72   }
73
74   private void updateSchemaContext(UpdateSchemaContext message) {
75     store.onGlobalContextUpdated(message.getSchemaContext());
76   }
77
78   private void registerChangeListener(RegisterChangeListener registerChangeListener) {
79     org.opendaylight.yangtools.concepts.ListenerRegistration<AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> registration =
80             store.registerChangeListener(registerChangeListener.getPath(), registerChangeListener.getListener(), registerChangeListener.getScope());
81     ActorRef listenerRegistration = getContext().actorOf(ListenerRegistration.props(registration));
82     getSender().tell(new RegisterChangeListenerReply(listenerRegistration.path()), getSelf());
83   }
84
85   private void createTransactionChain() {
86     DOMStoreTransactionChain chain = store.createTransactionChain();
87     ActorRef transactionChain = getContext().actorOf(ShardTransactionChain.props(chain));
88     getSender().tell(new CreateTransactionChainReply(transactionChain.path()), getSelf());
89   }
90 }