Merge "Add missing copyright text"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / modification / ModificationPayload.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.modification;
9
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;
19 import java.util.Map;
20 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
21 import org.opendaylight.controller.protobuff.messages.cluster.raft.AppendEntriesMessages.AppendEntries.ReplicatedLogEntry;
22
23 /**
24  * Payload implementation for MutableCompositeModification used for persistence and replication.
25  *
26  * @author Thomas Pantelis
27  */
28 public class ModificationPayload extends Payload implements Externalizable {
29     private static final long serialVersionUID = 1L;
30
31     private transient byte[] serializedPayload;
32
33     public ModificationPayload() {
34     }
35
36     public ModificationPayload(Modification from) throws IOException {
37         ByteArrayOutputStream bos = new ByteArrayOutputStream();
38         ObjectOutputStream out = new ObjectOutputStream(bos);
39         out.writeObject(from);
40         out.close();
41         serializedPayload = bos.toByteArray();
42     }
43
44     public Modification getModification() throws IOException, ClassNotFoundException {
45         ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(serializedPayload));
46         Modification to = (Modification) in.readObject();
47         in.close();
48         return to;
49     }
50
51     @Override
52     public int size() {
53         return serializedPayload.length;
54     }
55
56     @Override
57     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
58         int size = in.readInt();
59         serializedPayload = new byte[size];
60         in.readFully(serializedPayload);
61     }
62
63     @Override
64     public void writeExternal(ObjectOutput out) throws IOException {
65         out.writeInt(serializedPayload.length);
66         out.write(serializedPayload);
67     }
68
69     @Override
70     @Deprecated
71     public <T> Map<GeneratedExtension, T> encode() {
72         return null;
73     }
74
75     @Override
76     @Deprecated
77     public Payload decode(ReplicatedLogEntry.Payload payload) {
78         return null;
79     }
80 }