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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.MoreObjects;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableMap.Builder;
17 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18 import java.io.Externalizable;
19 import java.io.IOException;
20 import java.io.ObjectInput;
21 import java.io.ObjectOutput;
22 import java.io.Serializable;
23 import java.io.StreamCorruptedException;
25 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
26 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
27 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
28 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeStreamVersion;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * An {@link AbstractVersionedShardDataTreeSnapshot} which contains additional metadata.
36 * @author Robert Varga
39 public final class MetadataShardDataTreeSnapshot extends AbstractVersionedShardDataTreeSnapshot
40 implements Serializable {
41 private static final class Proxy implements Externalizable {
42 private static final long serialVersionUID = 1L;
43 private static final Logger LOG = LoggerFactory.getLogger(MetadataShardDataTreeSnapshot.class);
45 private Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
46 private NormalizedNodeStreamVersion version;
47 private NormalizedNode<?, ?> rootNode;
49 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
50 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
51 @SuppressWarnings("checkstyle:RedundantModifier")
56 Proxy(final MetadataShardDataTreeSnapshot snapshot) {
57 this.rootNode = snapshot.getRootNode().get();
58 this.metadata = snapshot.getMetadata();
59 this.version = snapshot.version().getStreamVersion();
63 public void writeExternal(final ObjectOutput out) throws IOException {
64 out.writeInt(metadata.size());
65 for (ShardDataTreeSnapshotMetadata<?> m : metadata.values()) {
68 out.writeBoolean(true);
69 try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out, version)) {
70 stream.writeNormalizedNode(rootNode);
75 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
76 final int metaSize = in.readInt();
77 checkArgument(metaSize >= 0, "Invalid negative metadata map length %s", metaSize);
79 // Default pre-allocate is 4, which should be fine
80 final Builder<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>>
81 metaBuilder = ImmutableMap.builder();
82 for (int i = 0; i < metaSize; ++i) {
83 final ShardDataTreeSnapshotMetadata<?> m = (ShardDataTreeSnapshotMetadata<?>) in.readObject();
85 metaBuilder.put(m.getType(), m);
87 LOG.warn("Skipping null metadata");
91 metadata = metaBuilder.build();
92 final boolean present = in.readBoolean();
94 throw new StreamCorruptedException("Unexpected missing root node");
97 final NormalizedNodeDataInput stream = NormalizedNodeInputOutput.newDataInput(in);
98 version = stream.getVersion();
99 rootNode = stream.readNormalizedNode();
102 private Object readResolve() {
103 return new MetadataShardDataTreeSnapshot(rootNode, metadata);
107 private static final long serialVersionUID = 1L;
109 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
110 + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
111 + "aren't serialized. FindBugs does not recognize this.")
112 private final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
114 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "See above justification.")
115 private final NormalizedNode<?, ?> rootNode;
117 public MetadataShardDataTreeSnapshot(final NormalizedNode<?, ?> rootNode) {
118 this(rootNode, ImmutableMap.of());
121 public MetadataShardDataTreeSnapshot(final NormalizedNode<?, ?> rootNode,
122 final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata) {
123 this.rootNode = requireNonNull(rootNode);
124 this.metadata = ImmutableMap.copyOf(metadata);
127 public Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> getMetadata() {
132 NormalizedNode<?, ?> rootNode() {
137 PayloadVersion version() {
138 return PayloadVersion.SODIUM_SR1;
141 private Object writeReplace() {
142 return new Proxy(this);
146 public String toString() {
147 return MoreObjects.toStringHelper(this).add("metadata", metadata).toString();