Bug 7521: Convert DatastoreSnapshot.ShardSnapshot to store Snapshot
[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 com.google.common.base.Verify;
11 import java.io.DataInputStream;
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectInputStream;
15 import java.io.ObjectOutput;
16 import java.util.Optional;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * An internally-versioned {@link ShardDataTreeSnapshot}. This class is an intermediate implementation-private
24  * class.
25  *
26  * @author Robert Varga
27  */
28 abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnapshot {
29     private static final Logger LOG = LoggerFactory.getLogger(AbstractVersionedShardDataTreeSnapshot.class);
30
31     @SuppressWarnings("checkstyle:FallThrough")
32     @Deprecated
33     static ShardDataTreeSnapshot deserializePreCarbon(final DataInputStream is) throws IOException {
34         final PayloadVersion version = PayloadVersion.readFrom(is);
35         switch (version) {
36             case BORON:
37                 // Boron snapshots use Java Serialization
38                 try (ObjectInputStream ois = new ObjectInputStream(is)) {
39                     return (ShardDataTreeSnapshot) ois.readObject();
40                 } catch (ClassNotFoundException e) {
41                     LOG.error("Failed to serialize data tree snapshot", e);
42                     throw new IOException("Snapshot failed to deserialize", e);
43                 }
44             case TEST_FUTURE_VERSION:
45             case TEST_PAST_VERSION:
46                 // These versions are never returned and this code is effectively dead
47                 break;
48             default:
49                 throw new IOException("Invalid payload version in snapshot");
50         }
51
52         // Not included as default in above switch to ensure we get warnings when new versions are added
53         throw new IOException("Encountered unhandled version" + version);
54     }
55
56     @SuppressWarnings("checkstyle:FallThrough")
57     static ShardDataTreeSnapshot versionedDeserialize(final ObjectInput in) throws IOException {
58         final PayloadVersion version = PayloadVersion.readFrom(in);
59         switch (version) {
60             case BORON:
61                 // Boron snapshots use Java Serialization
62                 try {
63                     return (ShardDataTreeSnapshot) in.readObject();
64                 } catch (ClassNotFoundException e) {
65                     LOG.error("Failed to serialize data tree snapshot", e);
66                     throw new IOException("Snapshot failed to deserialize", e);
67                 }
68             case TEST_FUTURE_VERSION:
69             case TEST_PAST_VERSION:
70                 // These versions are never returned and this code is effectively dead
71                 break;
72             default:
73                 throw new IOException("Invalid payload version in snapshot");
74         }
75
76         // Not included as default in above switch to ensure we get warnings when new versions are added
77         throw new IOException("Encountered unhandled version" + version);
78     }
79
80     @Override
81     public final Optional<NormalizedNode<?, ?>> getRootNode() {
82         return Optional.of(Verify.verifyNotNull(rootNode(), "Snapshot %s returned non-present root node", getClass()));
83     }
84
85     /**
86      * Return the root node.
87      *
88      * @return The root node.
89      */
90     @Nonnull
91     abstract NormalizedNode<?, ?> rootNode();
92
93     /**
94      * Return the snapshot payload version. Implementations of this method should return a constant.
95      *
96      * @return Snapshot payload version
97      */
98     @Nonnull
99     abstract PayloadVersion version();
100
101     private void versionedSerialize(final ObjectOutput out, final PayloadVersion version) throws IOException {
102         switch (version) {
103             case BORON:
104                 // Boron snapshots use Java Serialization
105                 out.writeObject(this);
106                 return;
107             case TEST_FUTURE_VERSION:
108             case TEST_PAST_VERSION:
109                 break;
110             default:
111                 throw new IOException("Invalid payload version in snapshot");
112         }
113
114         throw new IOException("Encountered unhandled version" + version);
115     }
116
117     @Override
118     public void serialize(final ObjectOutput out) throws IOException {
119         final PayloadVersion version = version();
120         version.writeTo(out);
121         versionedSerialize(out, version);
122     }
123 }

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