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.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;
21 * An internally-versioned {@link ShardDataTreeSnapshot}. This class is an intermediate implementation-private
24 * @author Robert Varga
26 abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnapshot {
27 private static final Logger LOG = LoggerFactory.getLogger(AbstractVersionedShardDataTreeSnapshot.class);
29 @SuppressWarnings("checkstyle:FallThrough")
30 static @NonNull ShardSnapshotState versionedDeserialize(final ObjectInput in) throws IOException {
31 final PayloadVersion version = PayloadVersion.readFrom(in);
35 return new ShardSnapshotState(readSnapshot(in), true);
38 return new ShardSnapshotState(readSnapshot(in), false);
39 case TEST_FUTURE_VERSION:
40 case TEST_PAST_VERSION:
41 // These versions are never returned and this code is effectively dead
43 // Not included as default in above switch to ensure we get warnings when new versions are added
44 throw new IOException("Encountered unhandled version" + version);
48 // Boron and Sodium snapshots use Java Serialization, but differ in stream format
49 private static @NonNull ShardDataTreeSnapshot readSnapshot(final ObjectInput in) throws IOException {
51 return (ShardDataTreeSnapshot) in.readObject();
52 } catch (ClassNotFoundException e) {
53 LOG.error("Failed to serialize data tree snapshot", e);
54 throw new IOException("Snapshot failed to deserialize", e);
59 public final Optional<NormalizedNode<?, ?>> getRootNode() {
60 return Optional.of(Verify.verifyNotNull(rootNode(), "Snapshot %s returned non-present root node", getClass()));
64 * Return the root node.
66 * @return The root node.
68 abstract @NonNull NormalizedNode<?, ?> rootNode();
71 * Return the snapshot payload version. Implementations of this method should return a constant.
73 * @return Snapshot payload version
75 abstract @NonNull PayloadVersion version();
77 private void versionedSerialize(final ObjectOutput out, final PayloadVersion version) throws IOException {
83 // Boron, NeonSR2, Sodium and Magnesium snapshots use Java Serialization, but differ in stream format
84 out.writeObject(this);
86 case TEST_FUTURE_VERSION:
87 case TEST_PAST_VERSION:
90 throw new IOException("Invalid payload version in snapshot");
93 throw new IOException("Encountered unhandled version" + version);
97 public void serialize(final ObjectOutput out) throws IOException {
98 final PayloadVersion version = version();
100 versionedSerialize(out, version);