2 * Copyright (c) 2015 Brocade Communications 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.modification;
10 import com.google.protobuf.GeneratedMessage.GeneratedExtension;
11 import java.io.ByteArrayInputStream;
12 import java.io.ByteArrayOutputStream;
13 import java.io.Externalizable;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectInputStream;
17 import java.io.ObjectOutput;
18 import java.io.ObjectOutputStream;
20 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
21 import org.opendaylight.controller.protobuff.messages.cluster.raft.AppendEntriesMessages.AppendEntries.ReplicatedLogEntry;
24 * Payload implementation for MutableCompositeModification used for persistence and replication.
26 * @author Thomas Pantelis
28 public class ModificationPayload extends Payload implements Externalizable {
29 private static final long serialVersionUID = 1L;
31 private transient byte[] serializedPayload;
33 public ModificationPayload() {
36 public ModificationPayload(Modification from) throws IOException {
37 ByteArrayOutputStream bos = new ByteArrayOutputStream();
38 ObjectOutputStream out = new ObjectOutputStream(bos);
39 out.writeObject(from);
41 serializedPayload = bos.toByteArray();
44 public Modification getModification() throws IOException, ClassNotFoundException {
45 ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(serializedPayload));
46 Modification to = (Modification) in.readObject();
53 return serializedPayload.length;
57 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
58 int size = in.readInt();
59 serializedPayload = new byte[size];
60 in.readFully(serializedPayload);
64 public void writeExternal(ObjectOutput out) throws IOException {
65 out.writeInt(serializedPayload.length);
66 out.write(serializedPayload);
69 @SuppressWarnings("rawtypes")
72 public <T> Map<GeneratedExtension, T> encode() {
78 public Payload decode(ReplicatedLogEntry.Payload payload) {