ae86921b926ebab030fd8b1f457a20ef11b5001b
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / persisted / UpdateElectionTerm.java
1 /*
2  * Copyright (c) 2016 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.raft.persisted;
9
10 import java.io.Externalizable;
11 import java.io.IOException;
12 import java.io.ObjectInput;
13 import java.io.ObjectOutput;
14 import java.io.Serializable;
15
16 /**
17  * Message class to persist election term information.
18  */
19 public class UpdateElectionTerm implements Serializable, MigratedSerializable {
20     private static final class Proxy implements Externalizable {
21         private static final long serialVersionUID = 1L;
22
23         private UpdateElectionTerm updateElectionTerm;
24
25         public Proxy() {
26             // For Externalizable
27         }
28
29         Proxy(final UpdateElectionTerm updateElectionTerm) {
30             this.updateElectionTerm = updateElectionTerm;
31         }
32
33         @Override
34         public void writeExternal(final ObjectOutput out) throws IOException {
35             out.writeLong(updateElectionTerm.currentTerm);
36             out.writeObject(updateElectionTerm.votedFor);
37          }
38
39         @Override
40         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
41             updateElectionTerm = new UpdateElectionTerm(in.readLong(), (String) in.readObject());
42         }
43
44         private Object readResolve() {
45             return updateElectionTerm;
46         }
47     }
48
49     private static final long serialVersionUID = 1L;
50
51     private final long currentTerm;
52     private final String votedFor;
53     private final boolean migrated;
54
55     private UpdateElectionTerm(final long currentTerm, final String votedFor, final boolean migrated) {
56         this.currentTerm = currentTerm;
57         this.votedFor = votedFor;
58         this.migrated = migrated;
59     }
60
61     public UpdateElectionTerm(final long currentTerm, final String votedFor) {
62         this(currentTerm, votedFor, false);
63     }
64
65     public long getCurrentTerm() {
66         return currentTerm;
67     }
68
69     public String getVotedFor() {
70         return votedFor;
71     }
72
73     @Override
74     public boolean isMigrated() {
75         return migrated;
76     }
77
78     @Override
79     public Object writeReplace() {
80         return new Proxy(this);
81     }
82
83     @Deprecated
84     public static UpdateElectionTerm createMigrated(final long currentTerm, final String votedFor) {
85         return new UpdateElectionTerm(currentTerm, votedFor, true);
86     }
87
88     @Override
89     public String toString() {
90         return "UpdateElectionTerm [currentTerm=" + currentTerm + ", votedFor=" + votedFor + "]";
91     }
92 }
93