a971ee6ad55f19f36d5e0b3c376b0d6765b64d6b
[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
21 /**
22  * @deprecated Deprecated in Boron in favor of CommitTransactionPayload
23  */
24 @Deprecated
25 final class DataTreeCandidatePayload extends Payload implements Externalizable {
26     private static final long serialVersionUID = 1L;
27
28     private transient byte[] serialized;
29
30     public DataTreeCandidatePayload() {
31         // Required by Externalizable
32     }
33
34     private DataTreeCandidatePayload(final byte[] serialized) {
35         this.serialized = Preconditions.checkNotNull(serialized);
36     }
37
38     /**
39      * @deprecated Use CommitTransactionPayload instead
40      */
41     @Deprecated
42     static DataTreeCandidatePayload create(final DataTreeCandidate candidate) {
43         final ByteArrayDataOutput out = ByteStreams.newDataOutput();
44         try {
45             DataTreeCandidateInputOutput.writeDataTreeCandidate(out, candidate);
46         } catch (IOException e) {
47             throw new IllegalArgumentException(String.format("Failed to serialize candidate %s", candidate), e);
48         }
49
50         return new DataTreeCandidatePayload(out.toByteArray());
51     }
52
53     public DataTreeCandidate getCandidate() throws IOException {
54         return DataTreeCandidateInputOutput.readDataTreeCandidate(ByteStreams.newDataInput(serialized));
55     }
56
57     @Override
58     public int size() {
59         return serialized.length;
60     }
61
62     @Override
63     public void writeExternal(final ObjectOutput out) throws IOException {
64         out.writeByte((byte)serialVersionUID);
65         out.writeInt(serialized.length);
66         out.write(serialized);
67     }
68
69     @Override
70     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
71         final long version = in.readByte();
72         Preconditions.checkArgument(version == serialVersionUID, "Unsupported serialization version %s", version);
73
74         final int length = in.readInt();
75         serialized = new byte[length];
76         in.readFully(serialized);
77     }
78 }