BUG-8159: add payload debugs
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / MetadataShardDataTreeSnapshot.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.persisted;
9
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;
22 import java.util.Map;
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;
27
28 /**
29  * An {@link AbstractVersionedShardDataTreeSnapshot} which contains additional metadata.
30  *
31  * @author Robert Varga
32  */
33 @Beta
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);
39
40         private Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
41         private NormalizedNode<?, ?> rootNode;
42
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")
46         public Proxy() {
47             // For Externalizable
48         }
49
50         Proxy(final MetadataShardDataTreeSnapshot snapshot) {
51             this.rootNode = snapshot.getRootNode().get();
52             this.metadata = snapshot.getMetadata();
53         }
54
55         @Override
56         public void writeExternal(final ObjectOutput out) throws IOException {
57             out.writeInt(metadata.size());
58             for (ShardDataTreeSnapshotMetadata<?> m : metadata.values()) {
59                 out.writeObject(m);
60             }
61
62             SerializationUtils.serializeNormalizedNode(rootNode, out);
63         }
64
65         @Override
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);
69
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();
75                 if (m != null) {
76                     metaBuilder.put(m.getType(), m);
77                 } else {
78                     LOG.warn("Skipping null metadata");
79                 }
80             }
81
82             metadata = metaBuilder.build();
83             rootNode = Verify.verifyNotNull(SerializationUtils.deserializeNormalizedNode(in));
84         }
85
86         private Object readResolve() {
87             return new MetadataShardDataTreeSnapshot(rootNode, metadata);
88         }
89     }
90
91     private static final long serialVersionUID = 1L;
92
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;
97
98     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "See above justification.")
99     private final NormalizedNode<?, ?> rootNode;
100
101     public MetadataShardDataTreeSnapshot(final NormalizedNode<?, ?> rootNode) {
102         this(rootNode, ImmutableMap.of());
103     }
104
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);
109     }
110
111     public Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> getMetadata() {
112         return metadata;
113     }
114
115     @Override
116     NormalizedNode<?, ?> rootNode() {
117         return rootNode;
118     }
119
120     @Override
121     PayloadVersion version() {
122         return PayloadVersion.BORON;
123     }
124
125     private Object writeReplace() {
126         return new Proxy(this);
127     }
128
129     @Override
130     public String toString() {
131         return MoreObjects.toStringHelper(this).add("metadata", metadata).toString();
132     }
133 }