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.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;
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 static ShardDataTreeSnapshot deserialize(final DataInputStream is) throws IOException {
33 final PayloadVersion version = PayloadVersion.readFrom(is);
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);
43 case TEST_FUTURE_VERSION:
44 case TEST_PAST_VERSION:
45 // These versions are never returned and this code is effectively dead
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);
54 public final Optional<NormalizedNode<?, ?>> getRootNode() {
55 return Optional.of(Verify.verifyNotNull(rootNode(), "Snapshot %s returned non-present root node", getClass()));
59 * Return the root node.
61 * @return The root node.
63 abstract @Nonnull NormalizedNode<?, ?> rootNode();
66 * Return the snapshot payload version. Implementations of this method should return a constant.
68 * @return Snapshot payload version
70 abstract @Nonnull PayloadVersion version();
72 private void versionedSerialize(final DataOutputStream dos, final PayloadVersion version) throws IOException {
75 // Boron snapshots use Java Serialization
76 try (ObjectOutputStream oos = new ObjectOutputStream(dos)) {
77 oos.writeObject(this);
80 case TEST_FUTURE_VERSION:
81 case TEST_PAST_VERSION:
86 throw new IOException("Encountered unhandled version" + version);
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();
95 versionedSerialize(dos, version);
98 return bos.toByteArray();