Merge "Neutron LBaaS v2.0 API support"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ThreePhaseCommitCohortProxy.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.ActorPath;
12 import akka.actor.ActorSelection;
13
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16
17 import org.opendaylight.controller.cluster.datastore.exceptions.TimeoutException;
18 import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction;
19 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
20 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
22 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
23 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
24 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction;
25 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
26 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.concurrent.Callable;
34
35 /**
36  * ThreePhaseCommitCohortProxy represents a set of remote cohort proxies
37  */
38 public class ThreePhaseCommitCohortProxy implements
39     DOMStoreThreePhaseCommitCohort{
40
41     private static final Logger
42         LOG = LoggerFactory.getLogger(DistributedDataStore.class);
43
44     private final ActorContext actorContext;
45     private final List<ActorPath> cohortPaths;
46     private final ListeningExecutorService executor;
47     private final String transactionId;
48
49
50     public ThreePhaseCommitCohortProxy(ActorContext actorContext,
51         List<ActorPath> cohortPaths,
52         String transactionId,
53         ListeningExecutorService executor) {
54
55         this.actorContext = actorContext;
56         this.cohortPaths = cohortPaths;
57         this.transactionId = transactionId;
58         this.executor = executor;
59     }
60
61     @Override public ListenableFuture<Boolean> canCommit() {
62         Callable<Boolean> call = new Callable<Boolean>() {
63
64             @Override
65             public Boolean call() throws Exception {
66                 for(ActorPath actorPath : cohortPaths){
67                     ActorSelection cohort = actorContext.actorSelection(actorPath);
68
69                     try {
70                         Object response =
71                                 actorContext.executeRemoteOperation(cohort,
72                                         new CanCommitTransaction().toSerializable(),
73                                         ActorContext.ASK_DURATION);
74
75                         if (response.getClass().equals(CanCommitTransactionReply.SERIALIZABLE_CLASS)) {
76                             CanCommitTransactionReply reply =
77                                     CanCommitTransactionReply.fromSerializable(response);
78                             if (!reply.getCanCommit()) {
79                                 return false;
80                             }
81                         }
82                     } catch(RuntimeException e){
83                         LOG.error("Unexpected Exception", e);
84                         return false;
85                     }
86                 }
87
88                 return true;
89             }
90         };
91
92         return executor.submit(call);
93     }
94
95     @Override public ListenableFuture<Void> preCommit() {
96         return voidOperation(new PreCommitTransaction().toSerializable(), PreCommitTransactionReply.SERIALIZABLE_CLASS);
97     }
98
99     @Override public ListenableFuture<Void> abort() {
100         return voidOperation(new AbortTransaction().toSerializable(), AbortTransactionReply.SERIALIZABLE_CLASS);
101     }
102
103     @Override public ListenableFuture<Void> commit() {
104         return voidOperation(new CommitTransaction().toSerializable(), CommitTransactionReply.SERIALIZABLE_CLASS);
105     }
106
107     private ListenableFuture<Void> voidOperation(final Object message, final Class expectedResponseClass){
108         Callable<Void> call = new Callable<Void>() {
109
110             @Override public Void call() throws Exception {
111                 for(ActorPath actorPath : cohortPaths){
112                     ActorSelection cohort = actorContext.actorSelection(actorPath);
113
114                     try {
115                         Object response =
116                             actorContext.executeRemoteOperation(cohort,
117                                 message,
118                                 ActorContext.ASK_DURATION);
119
120                         if (response != null && !response.getClass()
121                             .equals(expectedResponseClass)) {
122                             throw new RuntimeException(
123                                 String.format(
124                                     "did not get the expected response \n\t\t expected : %s \n\t\t actual   : %s",
125                                     expectedResponseClass.toString(),
126                                     response.getClass().toString())
127                             );
128                         }
129                     } catch(TimeoutException e){
130                         LOG.error(String.format("A timeout occurred when processing operation : %s", message));
131                     }
132                 }
133                 return null;
134             }
135         };
136
137         return executor.submit(call);
138     }
139
140     public List<ActorPath> getCohortPaths() {
141         return Collections.unmodifiableList(this.cohortPaths);
142     }
143 }