ec78d6045cffe09af69bbefe43a75030b72837f9
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / CloseLocalHistoryPayload.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 closed cleanly. It contains a {@link LocalHistoryIdentifier}.
20  *
21  * @author Robert Varga
22  */
23 public final class CloseLocalHistoryPayload extends AbstractIdentifiablePayload<LocalHistoryIdentifier> {
24     private static final class Proxy extends AbstractProxy<LocalHistoryIdentifier> {
25         private static final long serialVersionUID = 1L;
26
27         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
28         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
29         @SuppressWarnings("checkstyle:RedundantModifier")
30         public Proxy() {
31             // For Externalizable
32         }
33
34         Proxy(final byte[] serialized) {
35             super(serialized);
36         }
37
38         @Override
39         protected LocalHistoryIdentifier readIdentifier(final DataInput in) throws IOException {
40             return LocalHistoryIdentifier.readFrom(in);
41         }
42
43         @Override
44         protected CloseLocalHistoryPayload createObject(final LocalHistoryIdentifier identifier,
45                 final byte[] serialized) {
46             return new CloseLocalHistoryPayload(identifier, serialized);
47         }
48     }
49
50     private static final Logger LOG = LoggerFactory.getLogger(CloseLocalHistoryPayload.class);
51     private static final long serialVersionUID = 1L;
52
53     CloseLocalHistoryPayload(final LocalHistoryIdentifier historyId, final byte[] serialized) {
54         super(historyId, serialized);
55     }
56
57     public static CloseLocalHistoryPayload create(final LocalHistoryIdentifier historyId) {
58         final ByteArrayDataOutput out = ByteStreams.newDataOutput();
59         try {
60             historyId.writeTo(out);
61         } catch (IOException e) {
62             // This should never happen
63             LOG.error("Failed to serialize {}", historyId, e);
64             throw new RuntimeException("Failed to serialize " + historyId, e);
65         }
66         return new CloseLocalHistoryPayload(historyId, out.toByteArray());
67     }
68
69     @Override
70     protected Proxy externalizableProxy(final byte[] serialized) {
71         return new Proxy(serialized);
72     }
73 }

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