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.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
27 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * An {@link AbstractVersionedShardDataTreeSnapshot} which contains additional metadata.
34 * @author Robert Varga
37 public final class MetadataShardDataTreeSnapshot extends AbstractVersionedShardDataTreeSnapshot
38 implements Serializable {
39 private static final class Proxy implements Externalizable {
41 private static final long serialVersionUID = 1L;
42 private static final Logger LOG = LoggerFactory.getLogger(MetadataShardDataTreeSnapshot.class);
44 private Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
45 private NormalizedNodeStreamVersion version;
46 private NormalizedNode rootNode;
48 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
49 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
50 @SuppressWarnings("checkstyle:RedundantModifier")
56 public void writeExternal(final ObjectOutput out) {
57 throw new UnsupportedOperationException();
61 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
62 final int metaSize = in.readInt();
63 checkArgument(metaSize >= 0, "Invalid negative metadata map length %s", metaSize);
65 // Default pre-allocate is 4, which should be fine
66 final Builder<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>>
67 metaBuilder = ImmutableMap.builder();
68 for (int i = 0; i < metaSize; ++i) {
69 final ShardDataTreeSnapshotMetadata<?> m = (ShardDataTreeSnapshotMetadata<?>) in.readObject();
71 metaBuilder.put(m.getType(), m);
73 LOG.warn("Skipping null metadata");
77 metadata = metaBuilder.build();
78 final boolean present = in.readBoolean();
80 throw new StreamCorruptedException("Unexpected missing root node");
83 final NormalizedNodeDataInput stream = NormalizedNodeDataInput.newDataInput(in);
84 version = stream.getVersion();
85 rootNode = stream.readNormalizedNode();
88 private Object readResolve() {
89 return new MetadataShardDataTreeSnapshot(rootNode, metadata);
94 private static final long serialVersionUID = 1L;
96 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
97 + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
98 + "aren't serialized. FindBugs does not recognize this.")
99 private final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
101 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "See above justification.")
102 private final NormalizedNode rootNode;
104 public MetadataShardDataTreeSnapshot(final NormalizedNode rootNode) {
105 this(rootNode, ImmutableMap.of());
108 public MetadataShardDataTreeSnapshot(final NormalizedNode rootNode,
109 final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata) {
110 this.rootNode = requireNonNull(rootNode);
111 this.metadata = ImmutableMap.copyOf(metadata);
114 public Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> getMetadata() {
119 NormalizedNode rootNode() {
124 PayloadVersion version() {
125 return PayloadVersion.CHLORINE_SR2;
129 private Object writeReplace() {
134 public String toString() {
135 return MoreObjects.toStringHelper(this).add("metadata", metadata).toString();