BUG 2138 - Do not fail on module-based default shard
[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  * Payload wrapper for a DataTreeCandidatePayload.
24  *
25  * @deprecated Deprecated in Boron in favor of CommitTransactionPayload
26  */
27 @Deprecated
28 final class DataTreeCandidatePayload extends Payload implements Externalizable, MigratedSerializable {
29     private static final long serialVersionUID = 1L;
30
31     private transient byte[] serialized;
32
33     // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
34     // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
35     @SuppressWarnings("checkstyle:RedundantModifier")
36     public DataTreeCandidatePayload() {
37         // Required by Externalizable
38     }
39
40     private DataTreeCandidatePayload(final byte[] serialized) {
41         this.serialized = Preconditions.checkNotNull(serialized);
42     }
43
44     /**
45      * Creates a DataTreeCandidatePayload.
46      *
47      * @deprecated Use CommitTransactionPayload instead
48      */
49     @Deprecated
50     static DataTreeCandidatePayload create(final DataTreeCandidate candidate) {
51         final ByteArrayDataOutput out = ByteStreams.newDataOutput();
52         try {
53             DataTreeCandidateInputOutput.writeDataTreeCandidate(out, candidate);
54         } catch (IOException e) {
55             throw new IllegalArgumentException(String.format("Failed to serialize candidate %s", candidate), e);
56         }
57
58         return new DataTreeCandidatePayload(out.toByteArray());
59     }
60
61     public DataTreeCandidate getCandidate() throws IOException {
62         return DataTreeCandidateInputOutput.readDataTreeCandidate(ByteStreams.newDataInput(serialized));
63     }
64
65     @Override
66     public int size() {
67         return serialized.length;
68     }
69
70     @Override
71     public void writeExternal(final ObjectOutput out) throws IOException {
72         out.writeByte((byte)serialVersionUID);
73         out.writeInt(serialized.length);
74         out.write(serialized);
75     }
76
77     @Override
78     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
79         final long version = in.readByte();
80         Preconditions.checkArgument(version == serialVersionUID, "Unsupported serialization version %s", version);
81
82         final int length = in.readInt();
83         serialized = new byte[length];
84         in.readFully(serialized);
85     }
86
87     @Override
88     public boolean isMigrated() {
89         return true;
90     }
91
92     @Deprecated
93     @Override
94     public Object writeReplace() {
95         // this is fine
96         return this;
97     }
98 }