Switch MetadataShardDataTreeSnapshot to new Proxy
[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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
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;
24 import java.util.Map;
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;
30
31 /**
32  * An {@link AbstractVersionedShardDataTreeSnapshot} which contains additional metadata.
33  *
34  * @author Robert Varga
35  */
36 @Beta
37 public final class MetadataShardDataTreeSnapshot extends AbstractVersionedShardDataTreeSnapshot
38         implements Serializable {
39     private static final class Proxy implements Externalizable {
40         @java.io.Serial
41         private static final long serialVersionUID = 1L;
42         private static final Logger LOG = LoggerFactory.getLogger(MetadataShardDataTreeSnapshot.class);
43
44         private Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
45         private NormalizedNodeStreamVersion version;
46         private NormalizedNode rootNode;
47
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")
51         public Proxy() {
52             // For Externalizable
53         }
54
55         @Override
56         public void writeExternal(final ObjectOutput out) {
57             throw new UnsupportedOperationException();
58         }
59
60         @Override
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);
64
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();
70                 if (m != null) {
71                     metaBuilder.put(m.getType(), m);
72                 } else {
73                     LOG.warn("Skipping null metadata");
74                 }
75             }
76
77             metadata = metaBuilder.build();
78             final boolean present = in.readBoolean();
79             if (!present) {
80                 throw new StreamCorruptedException("Unexpected missing root node");
81             }
82
83             final NormalizedNodeDataInput stream = NormalizedNodeDataInput.newDataInput(in);
84             version = stream.getVersion();
85             rootNode = stream.readNormalizedNode();
86         }
87
88         private Object readResolve() {
89             return new MetadataShardDataTreeSnapshot(rootNode, metadata);
90         }
91     }
92
93     @java.io.Serial
94     private static final long serialVersionUID = 1L;
95
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;
100
101     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "See above justification.")
102     private final NormalizedNode rootNode;
103
104     public MetadataShardDataTreeSnapshot(final NormalizedNode rootNode) {
105         this(rootNode, ImmutableMap.of());
106     }
107
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);
112     }
113
114     public Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> getMetadata() {
115         return metadata;
116     }
117
118     @Override
119     NormalizedNode rootNode() {
120         return rootNode;
121     }
122
123     @Override
124     PayloadVersion version() {
125         return PayloadVersion.CHLORINE_SR2;
126     }
127
128     @java.io.Serial
129     private Object writeReplace() {
130         return new MS(this);
131     }
132
133     @Override
134     public String toString() {
135         return MoreObjects.toStringHelper(this).add("metadata", metadata).toString();
136     }
137 }