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.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectInputStream;
15 import java.io.ObjectOutput;
16 import java.util.Optional;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * An internally-versioned {@link ShardDataTreeSnapshot}. This class is an intermediate implementation-private
26 * @author Robert Varga
28 abstract class AbstractVersionedShardDataTreeSnapshot extends ShardDataTreeSnapshot {
29 private static final Logger LOG = LoggerFactory.getLogger(AbstractVersionedShardDataTreeSnapshot.class);
31 @SuppressWarnings("checkstyle:FallThrough")
33 static ShardDataTreeSnapshot deserializePreCarbon(final DataInputStream is) throws IOException {
34 final PayloadVersion version = PayloadVersion.readFrom(is);
37 // Boron snapshots use Java Serialization
38 try (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);
56 @SuppressWarnings("checkstyle:FallThrough")
57 static ShardDataTreeSnapshot versionedDeserialize(final ObjectInput in) throws IOException {
58 final PayloadVersion version = PayloadVersion.readFrom(in);
61 // Boron snapshots use Java Serialization
63 return (ShardDataTreeSnapshot) in.readObject();
64 } catch (ClassNotFoundException e) {
65 LOG.error("Failed to serialize data tree snapshot", e);
66 throw new IOException("Snapshot failed to deserialize", e);
68 case TEST_FUTURE_VERSION:
69 case TEST_PAST_VERSION:
70 // These versions are never returned and this code is effectively dead
73 throw new IOException("Invalid payload version in snapshot");
76 // Not included as default in above switch to ensure we get warnings when new versions are added
77 throw new IOException("Encountered unhandled version" + version);
81 public final Optional<NormalizedNode<?, ?>> getRootNode() {
82 return Optional.of(Verify.verifyNotNull(rootNode(), "Snapshot %s returned non-present root node", getClass()));
86 * Return the root node.
88 * @return The root node.
91 abstract NormalizedNode<?, ?> rootNode();
94 * Return the snapshot payload version. Implementations of this method should return a constant.
96 * @return Snapshot payload version
99 abstract PayloadVersion version();
101 private void versionedSerialize(final ObjectOutput out, final PayloadVersion version) throws IOException {
104 // Boron snapshots use Java Serialization
105 out.writeObject(this);
107 case TEST_FUTURE_VERSION:
108 case TEST_PAST_VERSION:
111 throw new IOException("Invalid payload version in snapshot");
114 throw new IOException("Encountered unhandled version" + version);
118 public void serialize(final ObjectOutput out) throws IOException {
119 final PayloadVersion version = version();
120 version.writeTo(out);
121 versionedSerialize(out, version);