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