Merge "Startup arch - remove artifactId prefix from dir names."
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / compat / BackwardsCompatibleThreePhaseCommitCohort.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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 package org.opendaylight.controller.cluster.datastore.compat;
9
10 import akka.actor.PoisonPill;
11 import akka.actor.Props;
12 import akka.japi.Creator;
13 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
14 import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction;
15 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
16 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
17 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction;
18 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * An actor to maintain backwards compatibility for the base Helium version where the 3-phase commit
24  * messages don't contain the transactionId. This actor just forwards a new message containing the
25  * transactionId to the parent Shard.
26  *
27  * @author Thomas Pantelis
28  */
29 public class BackwardsCompatibleThreePhaseCommitCohort extends AbstractUntypedActor {
30
31     private static final Logger LOG = LoggerFactory.getLogger(BackwardsCompatibleThreePhaseCommitCohort.class);
32
33     private final String transactionId;
34
35     private BackwardsCompatibleThreePhaseCommitCohort(String transactionId) {
36         this.transactionId = transactionId;
37     }
38
39     @Override
40     public void handleReceive(Object message) throws Exception {
41         if(message.getClass().equals(CanCommitTransaction.SERIALIZABLE_CLASS)) {
42             LOG.debug("BackwardsCompatibleThreePhaseCommitCohort CanCommitTransaction");
43
44             getContext().parent().forward(new CanCommitTransaction(transactionId).toSerializable(),
45                     getContext());
46         } else if(message.getClass().equals(PreCommitTransaction.SERIALIZABLE_CLASS)) {
47             LOG.debug("BackwardsCompatibleThreePhaseCommitCohort PreCommitTransaction");
48
49             // The Shard doesn't need the PreCommitTransaction message so just return the reply here.
50             getSender().tell(new PreCommitTransactionReply().toSerializable(), self());
51         } else if(message.getClass().equals(CommitTransaction.SERIALIZABLE_CLASS)) {
52             LOG.debug("BackwardsCompatibleThreePhaseCommitCohort CommitTransaction");
53
54             getContext().parent().forward(new CommitTransaction(transactionId).toSerializable(),
55                     getContext());
56
57             // We're done now - we can self-destruct
58             self().tell(PoisonPill.getInstance(), self());
59         } else if(message.getClass().equals(AbortTransaction.SERIALIZABLE_CLASS)) {
60             LOG.debug("BackwardsCompatibleThreePhaseCommitCohort AbortTransaction");
61
62             getContext().parent().forward(new AbortTransaction(transactionId).toSerializable(),
63                     getContext());
64             self().tell(PoisonPill.getInstance(), self());
65         }
66     }
67
68     public static Props props(String transactionId) {
69         return Props.create(new BackwardsCompatibleThreePhaseCommitCohortCreator(transactionId));
70     }
71
72     private static class BackwardsCompatibleThreePhaseCommitCohortCreator
73                                   implements Creator<BackwardsCompatibleThreePhaseCommitCohort> {
74         private static final long serialVersionUID = 1L;
75
76         private final String transactionId;
77
78         BackwardsCompatibleThreePhaseCommitCohortCreator(String transactionId) {
79             this.transactionId = transactionId;
80         }
81
82         @Override
83         public BackwardsCompatibleThreePhaseCommitCohort create() throws Exception {
84             return new BackwardsCompatibleThreePhaseCommitCohort(transactionId);
85         }
86     }
87 }