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