Merge "BUG 1211 - deletion non existing target returns 500"
[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.Persistent;
17 import akka.persistence.UntypedProcessor;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import com.google.common.util.concurrent.ListeningExecutorService;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
22 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
23 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
24 import org.opendaylight.controller.cluster.datastore.messages.ForwardedCommitTransaction;
25 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
26 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
27 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
28 import org.opendaylight.controller.cluster.datastore.modification.Modification;
29 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
30 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
33 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35
36 import java.util.HashMap;
37 import java.util.Map;
38 import java.util.concurrent.ExecutionException;
39 import java.util.concurrent.Executors;
40
41 /**
42  * A Shard represents a portion of the logical data tree <br/>
43  * <p>
44  * Our Shard uses InMemoryDataStore as it's internal representation and delegates all requests it
45  * </p>
46  */
47 public class Shard extends UntypedProcessor {
48
49   public static final String DEFAULT_NAME = "default";
50
51   private final ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
52
53   private final InMemoryDOMDataStore store;
54
55   private final Map<Modification, DOMStoreThreePhaseCommitCohort> modificationToCohort = new HashMap<>();
56
57   private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
58
59   private Shard(String name){
60     store = new InMemoryDOMDataStore(name, storeExecutor);
61   }
62
63   public static Props props(final String name) {
64     return Props.create(new Creator<Shard>() {
65
66       @Override
67       public Shard create() throws Exception {
68         return new Shard(name);
69       }
70
71     });
72   }
73
74   @Override
75   public void onReceive(Object message) throws Exception {
76     if (message instanceof CreateTransactionChain) {
77       createTransactionChain();
78     } else if (message instanceof RegisterChangeListener) {
79       registerChangeListener((RegisterChangeListener) message);
80     } else if (message instanceof UpdateSchemaContext) {
81       updateSchemaContext((UpdateSchemaContext) message);
82     } else if (message instanceof ForwardedCommitTransaction ) {
83       handleForwardedCommit((ForwardedCommitTransaction) message);
84     } else if (message instanceof Persistent){
85       commit((Persistent) message);
86     }
87   }
88
89   private void commit(Persistent message) {
90     Modification modification = (Modification) message.payload();
91     DOMStoreThreePhaseCommitCohort cohort = modificationToCohort.remove(modification);
92     if(cohort == null){
93       log.error("Could not find cohort for modification : " + modification);
94       return;
95     }
96     final ListenableFuture<Void> future = cohort.commit();
97     final ActorRef sender = getSender();
98     final ActorRef self = getSelf();
99     future.addListener(new Runnable() {
100       @Override
101       public void run() {
102         try {
103           future.get();
104           sender.tell(new CommitTransactionReply(), self);
105         } catch (InterruptedException | ExecutionException e) {
106           log.error(e, "An exception happened when committing");
107         }
108       }
109     }, getContext().dispatcher());
110   }
111
112   private void handleForwardedCommit(ForwardedCommitTransaction message) {
113     log.info("received forwarded transaction");
114     modificationToCohort.put(message.getModification(), message.getCohort());
115     getSelf().forward(Persistent.create(message.getModification()), getContext());
116   }
117
118   private void updateSchemaContext(UpdateSchemaContext message) {
119     store.onGlobalContextUpdated(message.getSchemaContext());
120   }
121
122   private void registerChangeListener(RegisterChangeListener registerChangeListener) {
123     org.opendaylight.yangtools.concepts.ListenerRegistration<AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>> registration =
124             store.registerChangeListener(registerChangeListener.getPath(), registerChangeListener.getListener(), registerChangeListener.getScope());
125     ActorRef listenerRegistration = getContext().actorOf(ListenerRegistration.props(registration));
126     getSender().tell(new RegisterChangeListenerReply(listenerRegistration.path()), getSelf());
127   }
128
129   private void createTransactionChain() {
130     DOMStoreTransactionChain chain = store.createTransactionChain();
131     ActorRef transactionChain = getContext().actorOf(ShardTransactionChain.props(chain));
132     getSender().tell(new CreateTransactionChainReply(transactionChain.path()), getSelf());
133   }
134 }