71e794b4d464bf266b06fe4243fe20ecc4f61068
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / PurgeTransactionPayload.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.TransactionIdentifier;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Payload persisted when a transaction is purged from the frontend. It contains the transaction identifier.
21  *
22  * @author Robert Varga
23  */
24 public final class PurgeTransactionPayload extends AbstractIdentifiablePayload<TransactionIdentifier> {
25     private static final class Proxy extends AbstractProxy<TransactionIdentifier> {
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 TransactionIdentifier readIdentifier(final DataInput in) throws IOException {
41             return TransactionIdentifier.readFrom(in);
42         }
43
44         @Override
45         protected PurgeTransactionPayload createObject(final TransactionIdentifier identifier,
46                 final byte[] serialized) {
47             return new PurgeTransactionPayload(identifier, serialized);
48         }
49     }
50
51     private static final Logger LOG = LoggerFactory.getLogger(PurgeTransactionPayload.class);
52     private static final long serialVersionUID = 1L;
53
54     PurgeTransactionPayload(final TransactionIdentifier transactionId, final byte[] serialized) {
55         super(transactionId, serialized);
56     }
57
58     public static PurgeTransactionPayload create(final TransactionIdentifier transactionId) {
59         final ByteArrayDataOutput out = ByteStreams.newDataOutput();
60         try {
61             transactionId.writeTo(out);
62         } catch (IOException e) {
63             // This should never happen
64             LOG.error("Failed to serialize {}", transactionId, e);
65             throw Throwables.propagate(e);
66         }
67         return new PurgeTransactionPayload(transactionId, out.toByteArray());
68     }
69
70     @Override
71     protected Proxy externalizableProxy(final byte[] serialized) {
72         return new Proxy(serialized);
73     }
74 }

©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.