2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
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;
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;
27 private transient byte[] serialized;
29 public DataTreeCandidatePayload() {
30 // Required by Externalizable
33 private DataTreeCandidatePayload(final byte[] serialized) {
34 this.serialized = Preconditions.checkNotNull(serialized);
37 static DataTreeCandidatePayload create(final DataTreeCandidate candidate) {
38 final ByteArrayDataOutput out = ByteStreams.newDataOutput();
40 DataTreeCandidateInputOutput.writeDataTreeCandidate(out, candidate);
41 } catch (IOException e) {
42 throw new IllegalArgumentException(String.format("Failed to serialize candidate %s", candidate), e);
45 return new DataTreeCandidatePayload(out.toByteArray());
49 DataTreeCandidate getCandidate() throws IOException {
50 return DataTreeCandidateInputOutput.readDataTreeCandidate(ByteStreams.newDataInput(serialized));
55 return serialized.length;
59 public void writeExternal(final ObjectOutput out) throws IOException {
60 out.writeByte((byte)serialVersionUID);
61 out.writeInt(serialized.length);
62 out.write(serialized);
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);
70 final int length = in.readInt();
71 serialized = new byte[length];
72 in.readFully(serialized);