Bump datastore version
[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 org.eclipse.jdt.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     static ShardDataTreeSnapshot versionedDeserialize(final ObjectInput in) throws IOException {
31         final PayloadVersion version = PayloadVersion.readFrom(in);
32         switch (version) {
33             case BORON:
34                 // Boron snapshots use Java Serialization
35                 try {
36                     return (ShardDataTreeSnapshot) in.readObject();
37                 } catch (ClassNotFoundException e) {
38                     LOG.error("Failed to serialize data tree snapshot", e);
39                     throw new IOException("Snapshot failed to deserialize", e);
40                 }
41             case TEST_FUTURE_VERSION:
42             case TEST_PAST_VERSION:
43                 // These versions are never returned and this code is effectively dead
44                 break;
45             default:
46                 throw new IOException("Invalid payload version in snapshot");
47         }
48
49         // Not included as default in above switch to ensure we get warnings when new versions are added
50         throw new IOException("Encountered unhandled version" + version);
51     }
52
53     @Override
54     public final Optional<NormalizedNode<?, ?>> getRootNode() {
55         return Optional.of(Verify.verifyNotNull(rootNode(), "Snapshot %s returned non-present root node", getClass()));
56     }
57
58     /**
59      * Return the root node.
60      *
61      * @return The root node.
62      */
63     abstract @NonNull NormalizedNode<?, ?> rootNode();
64
65     /**
66      * Return the snapshot payload version. Implementations of this method should return a constant.
67      *
68      * @return Snapshot payload version
69      */
70     abstract @NonNull PayloadVersion version();
71
72     private void versionedSerialize(final ObjectOutput out, final PayloadVersion version) throws IOException {
73         switch (version) {
74             case BORON:
75                 // Boron snapshots use Java Serialization
76                 out.writeObject(this);
77                 return;
78             case TEST_FUTURE_VERSION:
79             case TEST_PAST_VERSION:
80                 break;
81             default:
82                 throw new IOException("Invalid payload version in snapshot");
83         }
84
85         throw new IOException("Encountered unhandled version" + version);
86     }
87
88     @Override
89     public void serialize(final ObjectOutput out) throws IOException {
90         final PayloadVersion version = version();
91         version.writeTo(out);
92         versionedSerialize(out, version);
93     }
94 }