Remove use of thread-local input
[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.collect.ImmutableMap;
14 import com.google.common.collect.ImmutableMap.Builder;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.io.Externalizable;
17 import java.io.IOException;
18 import java.io.ObjectInput;
19 import java.io.ObjectOutput;
20 import java.io.Serializable;
21 import java.util.Map;
22 import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * An {@link AbstractVersionedShardDataTreeSnapshot} which contains additional metadata.
29  *
30  * @author Robert Varga
31  */
32 @Beta
33 public final class MetadataShardDataTreeSnapshot extends AbstractVersionedShardDataTreeSnapshot
34         implements Serializable {
35     private static final class Proxy implements Externalizable {
36         private static final long serialVersionUID = 1L;
37         private static final Logger LOG = LoggerFactory.getLogger(MetadataShardDataTreeSnapshot.class);
38
39         private Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
40         private NormalizedNode<?, ?> rootNode;
41
42         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
43         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
44         @SuppressWarnings("checkstyle:RedundantModifier")
45         public Proxy() {
46             // For Externalizable
47         }
48
49         Proxy(final MetadataShardDataTreeSnapshot snapshot) {
50             this.rootNode = snapshot.getRootNode().get();
51             this.metadata = snapshot.getMetadata();
52         }
53
54         @Override
55         public void writeExternal(final ObjectOutput out) throws IOException {
56             out.writeInt(metadata.size());
57             for (ShardDataTreeSnapshotMetadata<?> m : metadata.values()) {
58                 out.writeObject(m);
59             }
60
61             SerializationUtils.writeNormalizedNode(out, rootNode);
62         }
63
64         @Override
65         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
66             final int metaSize = in.readInt();
67             Preconditions.checkArgument(metaSize >= 0, "Invalid negative metadata map length %s", metaSize);
68
69             // Default pre-allocate is 4, which should be fine
70             final Builder<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>>
71                     metaBuilder = ImmutableMap.builder();
72             for (int i = 0; i < metaSize; ++i) {
73                 final ShardDataTreeSnapshotMetadata<?> m = (ShardDataTreeSnapshotMetadata<?>) in.readObject();
74                 if (m != null) {
75                     metaBuilder.put(m.getType(), m);
76                 } else {
77                     LOG.warn("Skipping null metadata");
78                 }
79             }
80
81             metadata = metaBuilder.build();
82             rootNode = SerializationUtils.readNormalizedNode(in).get();
83         }
84
85         private Object readResolve() {
86             return new MetadataShardDataTreeSnapshot(rootNode, metadata);
87         }
88     }
89
90     private static final long serialVersionUID = 1L;
91
92     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
93             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
94             + "aren't serialized. FindBugs does not recognize this.")
95     private final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
96
97     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "See above justification.")
98     private final NormalizedNode<?, ?> rootNode;
99
100     public MetadataShardDataTreeSnapshot(final NormalizedNode<?, ?> rootNode) {
101         this(rootNode, ImmutableMap.of());
102     }
103
104     public MetadataShardDataTreeSnapshot(final NormalizedNode<?, ?> rootNode,
105             final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata) {
106         this.rootNode = Preconditions.checkNotNull(rootNode);
107         this.metadata = ImmutableMap.copyOf(metadata);
108     }
109
110     public Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> getMetadata() {
111         return metadata;
112     }
113
114     @Override
115     NormalizedNode<?, ?> rootNode() {
116         return rootNode;
117     }
118
119     @Override
120     PayloadVersion version() {
121         return PayloadVersion.BORON;
122     }
123
124     private Object writeReplace() {
125         return new Proxy(this);
126     }
127
128     @Override
129     public String toString() {
130         return MoreObjects.toStringHelper(this).add("metadata", metadata).toString();
131     }
132 }