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