4692236532d4829f9a7e19d9ac9ca3abda334ff0
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / AbstractIdentifiablePayload.java
1 /*
2  * Copyright (c) 2016 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.persisted;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.io.ByteStreams;
13 import java.io.DataInput;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import java.io.Serializable;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
21 import org.opendaylight.yangtools.concepts.Identifiable;
22 import org.opendaylight.yangtools.concepts.Identifier;
23
24 /**
25  * Abstract base class for {@link Payload}s which hold a single {@link Identifier}.
26  *
27  * @author Robert Varga
28  */
29 public abstract class AbstractIdentifiablePayload<T extends Identifier>
30         extends Payload implements Identifiable<T>, Serializable {
31     protected abstract static class AbstractProxy<T extends Identifier> implements Externalizable {
32         private static final long serialVersionUID = 1L;
33         private byte[] serialized;
34         private T identifier;
35
36         public AbstractProxy() {
37             // For Externalizable
38         }
39
40         protected AbstractProxy(final byte[] serialized) {
41             this.serialized = Preconditions.checkNotNull(serialized);
42         }
43
44         @Override
45         public final void writeExternal(final ObjectOutput out) throws IOException {
46             out.writeInt(serialized.length);
47             out.write(serialized);
48         }
49
50         @Override
51         public final void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
52             final int length = in.readInt();
53             serialized = new byte[length];
54             in.readFully(serialized);
55             identifier = Verify.verifyNotNull(readIdentifier(ByteStreams.newDataInput(serialized)));
56         }
57
58         protected final Object readResolve() {
59             return Verify.verifyNotNull(createObject(identifier, serialized));
60         }
61
62         @Nonnull
63         protected abstract T readIdentifier(@Nonnull DataInput in) throws IOException;
64
65         @Nonnull
66         protected abstract Identifiable<T> createObject(@Nonnull T identifier, @Nonnull byte[] serialized);
67     }
68
69     private static final long serialVersionUID = 1L;
70     private final byte[] serialized;
71     private final T identifier;
72
73     AbstractIdentifiablePayload(@Nonnull final T identifier, @Nonnull final byte[] serialized) {
74         this.identifier = Preconditions.checkNotNull(identifier);
75         this.serialized = Preconditions.checkNotNull(serialized);
76     }
77
78     @Override
79     public final T getIdentifier() {
80         return identifier;
81     }
82
83     @Override
84     public final int size() {
85         return serialized.length;
86     }
87
88     protected final Object writeReplace() {
89         return Verify.verifyNotNull(externalizableProxy(serialized));
90     }
91
92     @Nonnull
93     protected abstract AbstractProxy<T> externalizableProxy(@Nonnull byte[] serialized);
94 }