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;
24 import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * An {@link AbstractVersionedShardDataTreeSnapshot} which contains additional metadata.
32 * @author Robert Varga
35 public final class MetadataShardDataTreeSnapshot extends AbstractVersionedShardDataTreeSnapshot
36 implements Serializable {
37 private static final class Proxy implements Externalizable {
38 private static final long serialVersionUID = 1L;
39 private static final Logger LOG = LoggerFactory.getLogger(MetadataShardDataTreeSnapshot.class);
41 private Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
42 private NormalizedNode<?, ?> rootNode;
44 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
45 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
46 @SuppressWarnings("checkstyle:RedundantModifier")
51 Proxy(final MetadataShardDataTreeSnapshot snapshot) {
52 this.rootNode = snapshot.getRootNode().get();
53 this.metadata = snapshot.getMetadata();
57 public void writeExternal(final ObjectOutput out) throws IOException {
58 out.writeInt(metadata.size());
59 for (ShardDataTreeSnapshotMetadata<?> m : metadata.values()) {
63 SerializationUtils.writeNormalizedNode(out, rootNode);
67 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
68 final int metaSize = in.readInt();
69 checkArgument(metaSize >= 0, "Invalid negative metadata map length %s", metaSize);
71 // Default pre-allocate is 4, which should be fine
72 final Builder<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>>
73 metaBuilder = ImmutableMap.builder();
74 for (int i = 0; i < metaSize; ++i) {
75 final ShardDataTreeSnapshotMetadata<?> m = (ShardDataTreeSnapshotMetadata<?>) in.readObject();
77 metaBuilder.put(m.getType(), m);
79 LOG.warn("Skipping null metadata");
83 metadata = metaBuilder.build();
84 rootNode = SerializationUtils.readNormalizedNode(in).get();
87 private Object readResolve() {
88 return new MetadataShardDataTreeSnapshot(rootNode, metadata);
92 private static final long serialVersionUID = 1L;
94 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
95 + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
96 + "aren't serialized. FindBugs does not recognize this.")
97 private final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
99 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "See above justification.")
100 private final NormalizedNode<?, ?> rootNode;
102 public MetadataShardDataTreeSnapshot(final NormalizedNode<?, ?> rootNode) {
103 this(rootNode, ImmutableMap.of());
106 public MetadataShardDataTreeSnapshot(final NormalizedNode<?, ?> rootNode,
107 final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata) {
108 this.rootNode = requireNonNull(rootNode);
109 this.metadata = ImmutableMap.copyOf(metadata);
112 public Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> getMetadata() {
117 NormalizedNode<?, ?> rootNode() {
122 PayloadVersion version() {
123 return PayloadVersion.SODIUM;
126 private Object writeReplace() {
127 return new Proxy(this);
131 public String toString() {
132 return MoreObjects.toStringHelper(this).add("metadata", metadata).toString();