BUG-5280: move DataTreeCandidate serialization to its own class
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DataTreeCandidatePayload.java
1 /*
2  * Copyright (c) 2015 Cisco 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.datastore;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.io.ByteArrayDataOutput;
12 import com.google.common.io.ByteStreams;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import org.opendaylight.controller.cluster.datastore.persisted.DataTreeCandidateInputOutput;
18 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 final class DataTreeCandidatePayload extends Payload implements Externalizable {
24     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidatePayload.class);
25     private static final long serialVersionUID = 1L;
26
27     private transient byte[] serialized;
28
29     public DataTreeCandidatePayload() {
30         // Required by Externalizable
31     }
32
33     private DataTreeCandidatePayload(final byte[] serialized) {
34         this.serialized = Preconditions.checkNotNull(serialized);
35     }
36
37     static DataTreeCandidatePayload create(final DataTreeCandidate candidate) {
38         final ByteArrayDataOutput out = ByteStreams.newDataOutput();
39         try {
40             DataTreeCandidateInputOutput.writeDataTreeCandidate(out, candidate);
41         } catch (IOException e) {
42             throw new IllegalArgumentException(String.format("Failed to serialize candidate %s", candidate), e);
43         }
44
45         return new DataTreeCandidatePayload(out.toByteArray());
46     }
47
48
49     DataTreeCandidate getCandidate() throws IOException {
50         return DataTreeCandidateInputOutput.readDataTreeCandidate(ByteStreams.newDataInput(serialized));
51     }
52
53     @Override
54     public int size() {
55         return serialized.length;
56     }
57
58     @Override
59     public void writeExternal(final ObjectOutput out) throws IOException {
60         out.writeByte((byte)serialVersionUID);
61         out.writeInt(serialized.length);
62         out.write(serialized);
63     }
64
65     @Override
66     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
67         final long version = in.readByte();
68         Preconditions.checkArgument(version == serialVersionUID, "Unsupported serialization version %s", version);
69
70         final int length = in.readInt();
71         serialized = new byte[length];
72         in.readFully(serialized);
73     }
74 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.