0667f7c0397d367956742d4affb7b326d52ca50d
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / AbstractVersionedShardDataTreeSnapshot.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 com.google.common.base.Verify.verifyNotNull;
11
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * An internally-versioned {@link ShardDataTreeSnapshot}. This class is an intermediate implementation-private
23  * class.
24  *
25  * @author Robert Varga
26  */
27 abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnapshot {
28     private static final Logger LOG = LoggerFactory.getLogger(AbstractVersionedShardDataTreeSnapshot.class);
29
30     @SuppressWarnings("checkstyle:FallThrough")
31     static @NonNull ShardSnapshotState versionedDeserialize(final ObjectInput in) throws IOException {
32         final PayloadVersion version = PayloadVersion.readFrom(in);
33         switch (version) {
34             case BORON:
35             case NEON_SR2:
36             case SODIUM_SR1:
37                 return new ShardSnapshotState(readSnapshot(in), true);
38             case MAGNESIUM:
39                 return new ShardSnapshotState(readSnapshot(in), false);
40             case TEST_FUTURE_VERSION:
41             case TEST_PAST_VERSION:
42                 // These versions are never returned and this code is effectively dead
43             default:
44                 // Not included as default in above switch to ensure we get warnings when new versions are added
45                 throw new IOException("Encountered unhandled version" + version);
46         }
47     }
48
49     // Boron and Sodium snapshots use Java Serialization, but differ in stream format
50     private static @NonNull ShardDataTreeSnapshot readSnapshot(final ObjectInput in) throws IOException {
51         try {
52             return (ShardDataTreeSnapshot) in.readObject();
53         } catch (ClassNotFoundException e) {
54             LOG.error("Failed to serialize data tree snapshot", e);
55             throw new IOException("Snapshot failed to deserialize", e);
56         }
57     }
58
59     @Override
60     public final Optional<NormalizedNode> getRootNode() {
61         return Optional.of(verifyNotNull(rootNode(), "Snapshot %s returned non-present root node", getClass()));
62     }
63
64     /**
65      * Return the root node.
66      *
67      * @return The root node.
68      */
69     abstract @NonNull NormalizedNode rootNode();
70
71     /**
72      * Return the snapshot payload version. Implementations of this method should return a constant.
73      *
74      * @return Snapshot payload version
75      */
76     abstract @NonNull PayloadVersion version();
77
78     private void versionedSerialize(final ObjectOutput out, final PayloadVersion version) throws IOException {
79         switch (version) {
80             case BORON:
81             case NEON_SR2:
82             case SODIUM_SR1:
83             case MAGNESIUM:
84                 // Boron, NeonSR2, Sodium and Magnesium snapshots use Java Serialization, but differ in stream format
85                 out.writeObject(this);
86                 return;
87             case TEST_FUTURE_VERSION:
88             case TEST_PAST_VERSION:
89                 break;
90             default:
91                 throw new IOException("Invalid payload version in snapshot");
92         }
93
94         throw new IOException("Encountered unhandled version" + version);
95     }
96
97     @Override
98     public void serialize(final ObjectOutput out) throws IOException {
99         final PayloadVersion version = version();
100         version.writeTo(out);
101         versionedSerialize(out, version);
102     }
103 }