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.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;
30 import java.util.concurrent.Executors;
33 * A Shard represents a portion of the logical data tree <br/>
35 * Our Shard uses InMemoryDataStore as it's internal representation and delegates all requests it
38 public class Shard extends UntypedProcessor {
40 public static final String DEFAULT_NAME = "default";
42 private final ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
44 private final InMemoryDOMDataStore store;
46 private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
48 private Shard(String name){
49 store = new InMemoryDOMDataStore(name, storeExecutor);
52 public static Props props(final String name) {
53 return Props.create(new Creator<Shard>() {
56 public Shard create() throws Exception {
57 return new Shard(name);
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);
74 private void updateSchemaContext(UpdateSchemaContext message) {
75 store.onGlobalContextUpdated(message.getSchemaContext());
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());
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());