Add RaftActorServerConfigurationSupport.raftActor
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / ServerConfigurationPayload.java
1 /*
2  * Copyright (c) 2015 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;
9
10 import com.google.common.base.Preconditions;
11 import com.google.protobuf.GeneratedMessage.GeneratedExtension;
12 import java.io.ByteArrayOutputStream;
13 import java.io.IOException;
14 import java.io.ObjectOutputStream;
15 import java.io.Serializable;
16 import java.util.List;
17 import java.util.Map;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
20 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.PersistentPayload;
21 import org.opendaylight.controller.protobuff.messages.cluster.raft.AppendEntriesMessages;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Payload data for server configuration log entries.
27  *
28  * @author Thomas Pantelis
29  */
30 public class ServerConfigurationPayload extends Payload implements PersistentPayload, Serializable {
31     private static final long serialVersionUID = 1L;
32
33     private static final Logger LOG = LoggerFactory.getLogger(ServerConfigurationPayload.class);
34
35     private final List<ServerInfo> serverConfig;
36     private transient int serializedSize = -1;
37
38     public ServerConfigurationPayload(@Nonnull List<ServerInfo> serverConfig) {
39         this.serverConfig = Preconditions.checkNotNull(serverConfig);
40     }
41
42     @Nonnull
43     public List<ServerInfo> getServerConfig() {
44         return serverConfig;
45     }
46
47     @Override
48     public int size() {
49         if(serializedSize < 0) {
50             try {
51                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
52                 ObjectOutputStream out = new ObjectOutputStream(bos);
53                 out.writeObject(serverConfig);
54                 out.close();
55
56                 serializedSize = bos.toByteArray().length;
57             } catch (IOException e) {
58                 serializedSize = 0;
59                 LOG.error("Error serializing", e);
60             }
61         }
62
63         return serializedSize;
64     }
65
66     @Override
67     @Deprecated
68     @SuppressWarnings("rawtypes")
69     public <T> Map<GeneratedExtension, T> encode() {
70         return null;
71     }
72
73     @Override
74     @Deprecated
75     public Payload decode(AppendEntriesMessages.AppendEntries.ReplicatedLogEntry.Payload payload) {
76         return null;
77     }
78
79     @Override
80     public String toString() {
81         return "ServerConfigurationPayload [serverConfig=" + serverConfig + "]";
82     }
83
84     public static class ServerInfo implements Serializable {
85         private static final long serialVersionUID = 1L;
86
87         private final String id;
88         private final boolean isVoting;
89
90         public ServerInfo(@Nonnull String id, boolean isVoting) {
91             this.id = Preconditions.checkNotNull(id);
92             this.isVoting = isVoting;
93         }
94
95         @Nonnull
96         public String getId() {
97             return id;
98         }
99
100         public boolean isVoting() {
101             return isVoting;
102         }
103
104         @Override
105         public int hashCode() {
106             final int prime = 31;
107             int result = 1;
108             result = prime * result + (isVoting ? 1231 : 1237);
109             result = prime * result + id.hashCode();
110             return result;
111         }
112
113         @Override
114         public boolean equals(Object obj) {
115             if (obj == null || getClass() != obj.getClass()) {
116                 return false;
117             }
118
119             ServerInfo other = (ServerInfo) obj;
120             return isVoting == other.isVoting && id.equals(other.id);
121         }
122
123         @Override
124         public String toString() {
125             return "ServerInfo [id=" + id + ", isVoting=" + isVoting + "]";
126         }
127     }
128 }