2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.persisted;
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;
24 * An internally-versioned {@link ShardDataTreeSnapshot}. This class is an intermediate implementation-private
27 * @author Robert Varga
29 abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnapshot {
30 private static final Logger LOG = LoggerFactory.getLogger(AbstractVersionedShardDataTreeSnapshot.class);
32 @SuppressWarnings("checkstyle:FallThrough")
33 static ShardDataTreeSnapshot deserialize(final DataInputStream is) throws IOException {
34 final PayloadVersion version = PayloadVersion.readFrom(is);
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);
44 case TEST_FUTURE_VERSION:
45 case TEST_PAST_VERSION:
46 // These versions are never returned and this code is effectively dead
49 throw new IOException("Invalid payload version in snapshot");
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);
57 public final Optional<NormalizedNode<?, ?>> getRootNode() {
58 return Optional.of(Verify.verifyNotNull(rootNode(), "Snapshot %s returned non-present root node", getClass()));
62 * Return the root node.
64 * @return The root node.
67 abstract NormalizedNode<?, ?> rootNode();
70 * Return the snapshot payload version. Implementations of this method should return a constant.
72 * @return Snapshot payload version
75 abstract PayloadVersion version();
77 private void versionedSerialize(final DataOutputStream dos, final PayloadVersion version) throws IOException {
80 // Boron snapshots use Java Serialization
81 try (ObjectOutputStream oos = new ObjectOutputStream(dos)) {
82 oos.writeObject(this);
85 case TEST_FUTURE_VERSION:
86 case TEST_PAST_VERSION:
89 throw new IOException("Invalid payload version in snapshot");
92 throw new IOException("Encountered unhandled version" + version);
96 public void serialize(final OutputStream os) throws IOException {
97 try (final DataOutputStream dos = new DataOutputStream(os)) {
98 final PayloadVersion version = version();
100 versionedSerialize(dos, version);