2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.raft;
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;
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;
26 * Payload data for server configuration log entries.
28 * @author Thomas Pantelis
30 public class ServerConfigurationPayload extends Payload implements PersistentPayload, Serializable {
31 private static final long serialVersionUID = 1L;
33 private static final Logger LOG = LoggerFactory.getLogger(ServerConfigurationPayload.class);
35 private final List<ServerInfo> serverConfig;
36 private transient int serializedSize = -1;
38 public ServerConfigurationPayload(@Nonnull List<ServerInfo> serverConfig) {
39 this.serverConfig = Preconditions.checkNotNull(serverConfig);
43 public List<ServerInfo> getServerConfig() {
49 if(serializedSize < 0) {
51 ByteArrayOutputStream bos = new ByteArrayOutputStream();
52 ObjectOutputStream out = new ObjectOutputStream(bos);
53 out.writeObject(serverConfig);
56 serializedSize = bos.toByteArray().length;
57 } catch (IOException e) {
59 LOG.error("Error serializing", e);
63 return serializedSize;
68 @SuppressWarnings("rawtypes")
69 public <T> Map<GeneratedExtension, T> encode() {
75 public Payload decode(AppendEntriesMessages.AppendEntries.ReplicatedLogEntry.Payload payload) {
80 public String toString() {
81 return "ServerConfigurationPayload [serverConfig=" + serverConfig + "]";
84 public static class ServerInfo implements Serializable {
85 private static final long serialVersionUID = 1L;
87 private final String id;
88 private final boolean isVoting;
90 public ServerInfo(@Nonnull String id, boolean isVoting) {
91 this.id = Preconditions.checkNotNull(id);
92 this.isVoting = isVoting;
96 public String getId() {
100 public boolean isVoting() {
105 public int hashCode() {
106 final int prime = 31;
108 result = prime * result + (isVoting ? 1231 : 1237);
109 result = prime * result + id.hashCode();
114 public boolean equals(Object obj) {
115 if (obj == null || getClass() != obj.getClass()) {
119 ServerInfo other = (ServerInfo) obj;
120 return isVoting == other.isVoting && id.equals(other.id);
124 public String toString() {
125 return "ServerInfo [id=" + id + ", isVoting=" + isVoting + "]";