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