1d9b58f27049046fdee8f62d2e690e2344439821
[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.DataOutputStream;
13 import java.io.IOException;
14 import java.io.ObjectInputStream;
15 import java.io.ObjectOutputStream;
16 import java.io.OutputStream;
17 import java.util.Optional;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * An internally-versioned {@link ShardDataTreeSnapshot}. This class is an intermediate implementation-private
25  * class.
26  *
27  * @author Robert Varga
28  */
29 abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnapshot {
30     private static final Logger LOG = LoggerFactory.getLogger(AbstractVersionedShardDataTreeSnapshot.class);
31
32     @SuppressWarnings("checkstyle:FallThrough")
33     static ShardDataTreeSnapshot deserialize(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 (final 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     @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 DataOutputStream dos, final PayloadVersion version) throws IOException {
78         switch (version) {
79             case BORON:
80                 // Boron snapshots use Java Serialization
81                 try (ObjectOutputStream oos = new ObjectOutputStream(dos)) {
82                     oos.writeObject(this);
83                 }
84                 return;
85             case TEST_FUTURE_VERSION:
86             case TEST_PAST_VERSION:
87                 break;
88             default:
89                 throw new IOException("Invalid payload version in snapshot");
90         }
91
92         throw new IOException("Encountered unhandled version" + version);
93     }
94
95     @Override
96     public void serialize(final OutputStream os) throws IOException {
97         try (final DataOutputStream dos = new DataOutputStream(os)) {
98             final PayloadVersion version = version();
99             version.writeTo(dos);
100             versionedSerialize(dos, version);
101         }
102     }
103 }