1dbab16c7ba118586040e196d43dc4cdeef636a8
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / ShardDataTreeSnapshot.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.annotations.Beta;
11 import java.io.ByteArrayInputStream;
12 import java.io.DataInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import java.util.Optional;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Abstract base class for snapshots of the ShardDataTree.
24  *
25  * @author Robert Varga
26  */
27 @Beta
28 public abstract class ShardDataTreeSnapshot {
29     private static final Logger LOG = LoggerFactory.getLogger(ShardDataTreeSnapshot.class);
30
31     ShardDataTreeSnapshot() {
32         // Hidden to prevent subclassing from outside of this package
33     }
34
35     @Deprecated
36     public static ShardDataTreeSnapshot deserializePreCarbon(final byte[] bytes) throws IOException {
37         try (InputStream is = new ByteArrayInputStream(bytes)) {
38             try (DataInputStream dis = new DataInputStream(is)) {
39                 final ShardDataTreeSnapshot ret = AbstractVersionedShardDataTreeSnapshot.deserializePreCarbon(dis);
40
41                 // Make sure we consume all bytes, otherwise something went very wrong
42                 final int bytesLeft = dis.available();
43                 if (bytesLeft != 0) {
44                     throw new IOException("Deserialization left " + bytesLeft + " in the buffer");
45                 }
46
47
48                 return ret;
49             }
50         }
51     }
52
53     public static ShardDataTreeSnapshot deserialize(final ObjectInput in) throws IOException {
54         final ShardDataTreeSnapshot ret = AbstractVersionedShardDataTreeSnapshot.versionedDeserialize(in);
55
56         // Make sure we consume all bytes, otherwise something went very wrong
57         final int bytesLeft = in.available();
58         if (bytesLeft != 0) {
59             throw new IOException("Deserialization left " + bytesLeft + " in the buffer");
60         }
61
62
63         return ret;
64     }
65
66     /**
67      * Get the root data node contained in this snapshot.
68      *
69      * @return An optional root node.
70      */
71     public abstract Optional<NormalizedNode<?, ?>> getRootNode();
72
73     public abstract void serialize(ObjectOutput out) throws IOException;
74 }
75