Add Payload.serializedSize()
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / PurgeLocalHistoryPayload.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.io.ByteArrayDataOutput;
11 import com.google.common.io.ByteStreams;
12 import java.io.DataInput;
13 import java.io.IOException;
14 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * Payload persisted when a local history is completely purged, i.e. the frontend has removed it from its tracking.
20  * It contains a {@link LocalHistoryIdentifier}.
21  *
22  * @author Robert Varga
23  */
24 public final class PurgeLocalHistoryPayload extends AbstractIdentifiablePayload<LocalHistoryIdentifier> {
25     private static final class Proxy extends AbstractProxy<LocalHistoryIdentifier> {
26         private static final long serialVersionUID = 1L;
27
28         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
29         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
30         @SuppressWarnings("checkstyle:RedundantModifier")
31         public Proxy() {
32             // For Externalizable
33         }
34
35         Proxy(final byte[] serialized) {
36             super(serialized);
37         }
38
39         @Override
40         protected LocalHistoryIdentifier readIdentifier(final DataInput in) throws IOException {
41             return LocalHistoryIdentifier.readFrom(in);
42         }
43
44         @Override
45         protected PurgeLocalHistoryPayload createObject(final LocalHistoryIdentifier identifier,
46                 final byte[] serialized) {
47             return new PurgeLocalHistoryPayload(identifier, serialized);
48         }
49     }
50
51     private static final Logger LOG = LoggerFactory.getLogger(PurgeLocalHistoryPayload.class);
52     private static final long serialVersionUID = 1L;
53     private static final int PROXY_SIZE = externalizableProxySize(Proxy::new);
54
55     PurgeLocalHistoryPayload(final LocalHistoryIdentifier historyId, final byte[] serialized) {
56         super(historyId, serialized);
57     }
58
59     public static PurgeLocalHistoryPayload create(final LocalHistoryIdentifier historyId,
60             final int initialSerializedBufferCapacity) {
61         final ByteArrayDataOutput out = ByteStreams.newDataOutput(initialSerializedBufferCapacity);
62         try {
63             historyId.writeTo(out);
64         } catch (IOException e) {
65             // This should never happen
66             LOG.error("Failed to serialize {}", historyId, e);
67             throw new IllegalStateException("Failed to serialize " + historyId, e);
68         }
69         return new PurgeLocalHistoryPayload(historyId, out.toByteArray());
70     }
71
72     @Override
73     protected Proxy externalizableProxy(final byte[] serialized) {
74         return new Proxy(serialized);
75     }
76
77     @Override
78     protected int externalizableProxySize() {
79         return PROXY_SIZE;
80     }
81 }