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.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;
28 import java.util.concurrent.Executors;
31 * A Shard represents a portion of the logical data tree
33 * Our Shard uses InMemoryDataStore as it's internal representation and delegates all requests it
36 public class Shard extends UntypedProcessor {
38 ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
40 private final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor);
42 LoggingAdapter log = Logging.getLogger(getContext().system(), this);
45 public void onReceive(Object message) throws Exception {
46 if (message instanceof CreateTransactionChain) {
47 createTransactionChain();
48 } else if(message instanceof RegisterChangeListener){
49 registerChangeListener((RegisterChangeListener) message);
50 } else if(message instanceof UpdateSchemaContext){
51 updateSchemaContext((UpdateSchemaContext) message);
55 private void updateSchemaContext(UpdateSchemaContext message) {
56 store.onGlobalContextUpdated(message.getSchemaContext());
59 private void registerChangeListener(RegisterChangeListener registerChangeListener) {
60 org.opendaylight.yangtools.concepts.ListenerRegistration<AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> registration =
61 store.registerChangeListener(registerChangeListener.getPath(), registerChangeListener.getListener(), registerChangeListener.getScope());
62 ActorRef listenerRegistration = getContext().actorOf(ListenerRegistration.props(registration));
63 getSender().tell(new RegisterChangeListenerReply(listenerRegistration.path()), getSelf());
66 private void createTransactionChain() {
67 DOMStoreTransactionChain chain = store.createTransactionChain();
68 ActorRef transactionChain = getContext().actorOf(ShardTransactionChain.props(chain));
69 getSender().tell(new CreateTransactionChainReply(transactionChain.path()), getSelf());