BUG-5280: centralize ShardSnapshot operations
[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.ByteArrayOutputStream;
12 import java.io.DataInputStream;
13 import java.io.DataOutputStream;
14 import java.io.IOException;
15 import java.io.ObjectInputStream;
16 import java.io.ObjectOutputStream;
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     static ShardDataTreeSnapshot deserialize(final DataInputStream is) throws IOException {
33         final PayloadVersion version = PayloadVersion.readFrom(is);
34         switch (version) {
35             case BORON:
36                 // Boron snapshots use Java Serialization
37                 try (final ObjectInputStream ois = new ObjectInputStream(is)) {
38                     return (ShardDataTreeSnapshot) ois.readObject();
39                 } catch (ClassNotFoundException e) {
40                     LOG.error("Failed to serialize data tree snapshot", e);
41                     throw new IOException("Snapshot failed to deserialize", e);
42                 }
43             case TEST_FUTURE_VERSION:
44             case TEST_PAST_VERSION:
45                 // These versions are never returned and this code is effectively dead
46                 break;
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 DataOutputStream dos, final PayloadVersion version) throws IOException {
73         switch (version) {
74             case BORON:
75                 // Boron snapshots use Java Serialization
76                 try (ObjectOutputStream oos = new ObjectOutputStream(dos)) {
77                     oos.writeObject(this);
78                 }
79                 return;
80             case TEST_FUTURE_VERSION:
81             case TEST_PAST_VERSION:
82                 break;
83
84         }
85
86         throw new IOException("Encountered unhandled version" + version);
87     }
88
89     @Override
90     public final byte[] serialize() throws IOException {
91         try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
92             try (final DataOutputStream dos = new DataOutputStream(bos)) {
93                 final PayloadVersion version = version();
94                 version.writeTo(dos);
95                 versionedSerialize(dos, version);
96             }
97
98             return bos.toByteArray();
99         }
100     }
101 }