f40438b269e2fdaf571e0d12b914ea38af3831e0
[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.persisted.MigratedSerializable;
19 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
21
22 /**
23  * @deprecated Deprecated in Boron in favor of CommitTransactionPayload
24  */
25 @Deprecated
26 final class DataTreeCandidatePayload extends Payload implements Externalizable, MigratedSerializable {
27     private static final long serialVersionUID = 1L;
28
29     private transient byte[] serialized;
30
31     public DataTreeCandidatePayload() {
32         // Required by Externalizable
33     }
34
35     private DataTreeCandidatePayload(final byte[] serialized) {
36         this.serialized = Preconditions.checkNotNull(serialized);
37     }
38
39     /**
40      * @deprecated Use CommitTransactionPayload instead
41      */
42     @Deprecated
43     static DataTreeCandidatePayload create(final DataTreeCandidate candidate) {
44         final ByteArrayDataOutput out = ByteStreams.newDataOutput();
45         try {
46             DataTreeCandidateInputOutput.writeDataTreeCandidate(out, candidate);
47         } catch (IOException e) {
48             throw new IllegalArgumentException(String.format("Failed to serialize candidate %s", candidate), e);
49         }
50
51         return new DataTreeCandidatePayload(out.toByteArray());
52     }
53
54     public DataTreeCandidate getCandidate() throws IOException {
55         return DataTreeCandidateInputOutput.readDataTreeCandidate(ByteStreams.newDataInput(serialized));
56     }
57
58     @Override
59     public int size() {
60         return serialized.length;
61     }
62
63     @Override
64     public void writeExternal(final ObjectOutput out) throws IOException {
65         out.writeByte((byte)serialVersionUID);
66         out.writeInt(serialized.length);
67         out.write(serialized);
68     }
69
70     @Override
71     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
72         final long version = in.readByte();
73         Preconditions.checkArgument(version == serialVersionUID, "Unsupported serialization version %s", version);
74
75         final int length = in.readInt();
76         serialized = new byte[length];
77         in.readFully(serialized);
78     }
79
80     @Override
81     public boolean isMigrated() {
82         return true;
83     }
84
85     @Deprecated
86     @Override
87     public Object writeReplace() {
88         // this is fine
89         return this;
90     }
91 }

©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.