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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.