25a7ee86229dcccf7b42a4c2d0eae1561c43ae12
[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 java.util.AbstractMap.SimpleImmutableEntry;
18 import java.util.Map.Entry;
19 import java.util.Optional;
20 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
21 import org.opendaylight.controller.cluster.datastore.persisted.DataTreeCandidateInputOutput;
22 import org.opendaylight.controller.cluster.datastore.persisted.DataTreeCandidateSupplier;
23 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
25
26 /**
27  * @deprecated Deprecated in Boron in favor of CommitTransactionPayload
28  */
29 @Deprecated
30 final class DataTreeCandidatePayload extends Payload implements DataTreeCandidateSupplier, Externalizable {
31     private static final long serialVersionUID = 1L;
32
33     private transient byte[] serialized;
34
35     public DataTreeCandidatePayload() {
36         // Required by Externalizable
37     }
38
39     private DataTreeCandidatePayload(final byte[] serialized) {
40         this.serialized = Preconditions.checkNotNull(serialized);
41     }
42
43     /**
44      * @deprecated Use CommitTransactionPayload instead
45      */
46     @Deprecated
47     static DataTreeCandidatePayload create(final DataTreeCandidate candidate) {
48         final ByteArrayDataOutput out = ByteStreams.newDataOutput();
49         try {
50             DataTreeCandidateInputOutput.writeDataTreeCandidate(out, candidate);
51         } catch (IOException e) {
52             throw new IllegalArgumentException(String.format("Failed to serialize candidate %s", candidate), e);
53         }
54
55         return new DataTreeCandidatePayload(out.toByteArray());
56     }
57
58
59     @Override
60     public Entry<Optional<TransactionIdentifier>, DataTreeCandidate> getCandidate() throws IOException {
61         return new SimpleImmutableEntry<>(Optional.empty(),
62                 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 }