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.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Verify;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.ImmutableMap.Builder;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.io.Externalizable;
18 import java.io.IOException;
19 import java.io.ObjectInput;
20 import java.io.ObjectOutput;
21 import java.io.Serializable;
23 import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * An {@link AbstractVersionedShardDataTreeSnapshot} which contains additional metadata.
31 * @author Robert Varga
34 public final class MetadataShardDataTreeSnapshot extends AbstractVersionedShardDataTreeSnapshot
35 implements Serializable {
36 private static final class Proxy implements Externalizable {
37 private static final long serialVersionUID = 1L;
38 private static final Logger LOG = LoggerFactory.getLogger(MetadataShardDataTreeSnapshot.class);
40 private Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
41 private NormalizedNode<?, ?> rootNode;
43 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
44 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
45 @SuppressWarnings("checkstyle:RedundantModifier")
50 Proxy(final MetadataShardDataTreeSnapshot snapshot) {
51 this.rootNode = snapshot.getRootNode().get();
52 this.metadata = snapshot.getMetadata();
56 public void writeExternal(final ObjectOutput out) throws IOException {
57 out.writeInt(metadata.size());
58 for (ShardDataTreeSnapshotMetadata<?> m : metadata.values()) {
62 SerializationUtils.serializeNormalizedNode(rootNode, out);
66 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
67 final int metaSize = in.readInt();
68 Preconditions.checkArgument(metaSize >= 0, "Invalid negative metadata map length %s", metaSize);
70 // Default pre-allocate is 4, which should be fine
71 final Builder<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>>
72 metaBuilder = ImmutableMap.builder();
73 for (int i = 0; i < metaSize; ++i) {
74 final ShardDataTreeSnapshotMetadata<?> m = (ShardDataTreeSnapshotMetadata<?>) in.readObject();
76 metaBuilder.put(m.getType(), m);
78 LOG.warn("Skipping null metadata");
82 metadata = metaBuilder.build();
83 rootNode = Verify.verifyNotNull(SerializationUtils.deserializeNormalizedNode(in));
86 private Object readResolve() {
87 return new MetadataShardDataTreeSnapshot(rootNode, metadata);
91 private static final long serialVersionUID = 1L;
93 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
94 + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
95 + "aren't serialized. FindBugs does not recognize this.")
96 private final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
98 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "See above justification.")
99 private final NormalizedNode<?, ?> rootNode;
101 public MetadataShardDataTreeSnapshot(final NormalizedNode<?, ?> rootNode) {
102 this(rootNode, ImmutableMap.of());
105 public MetadataShardDataTreeSnapshot(final NormalizedNode<?, ?> rootNode,
106 final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata) {
107 this.rootNode = Preconditions.checkNotNull(rootNode);
108 this.metadata = ImmutableMap.copyOf(metadata);
111 public Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> getMetadata() {
116 NormalizedNode<?, ?> rootNode() {
121 PayloadVersion version() {
122 return PayloadVersion.BORON;
125 private Object writeReplace() {
126 return new Proxy(this);
130 public String toString() {
131 return MoreObjects.toStringHelper(this).add("metadata", metadata).toString();