Initial code for RaftActorServerConfigurationSupport
[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.protobuf.GeneratedMessage.GeneratedExtension;
11 import java.io.ByteArrayOutputStream;
12 import java.io.IOException;
13 import java.io.ObjectOutputStream;
14 import java.io.Serializable;
15 import java.util.List;
16 import java.util.Map;
17 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
18 import org.opendaylight.controller.protobuff.messages.cluster.raft.AppendEntriesMessages;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Payload data for server configuration log entries.
24  *
25  * @author Thomas Pantelis
26  */
27 public class ServerConfigurationPayload extends Payload implements Serializable {
28     private static final long serialVersionUID = 1L;
29
30     private static final Logger LOG = LoggerFactory.getLogger(ServerConfigurationPayload.class);
31
32     private final List<String> newServerConfig;
33     private final List<String> oldServerConfig;
34     private transient int serializedSize = -1;
35
36     public ServerConfigurationPayload(List<String> newServerConfig, List<String> oldServerConfig) {
37         this.newServerConfig = newServerConfig;
38         this.oldServerConfig = oldServerConfig;
39     }
40
41     public List<String> getNewServerConfig() {
42         return newServerConfig;
43     }
44
45
46     public List<String> getOldServerConfig() {
47         return oldServerConfig;
48     }
49
50     @Override
51     public int size() {
52         if(serializedSize < 0) {
53             try {
54                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
55                 ObjectOutputStream out = new ObjectOutputStream(bos);
56                 out.writeObject(newServerConfig);
57                 out.writeObject(oldServerConfig);
58                 out.close();
59
60                 serializedSize = bos.toByteArray().length;
61             } catch (IOException e) {
62                 serializedSize = 0;
63                 LOG.error("Error serializing", e);
64             }
65         }
66
67         return serializedSize;
68     }
69
70     @Override
71     @Deprecated
72     @SuppressWarnings("rawtypes")
73     public <T> Map<GeneratedExtension, T> encode() {
74         return null;
75     }
76
77     @Override
78     @Deprecated
79     public Payload decode(AppendEntriesMessages.AppendEntries.ReplicatedLogEntry.Payload payload) {
80         return null;
81     }
82 }