Rename SODIUM versions to NEON_SR2
[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.util.Map;
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;
28
29 /**
30  * An {@link AbstractVersionedShardDataTreeSnapshot} which contains additional metadata.
31  *
32  * @author Robert Varga
33  */
34 @Beta
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);
40
41         private Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
42         private NormalizedNode<?, ?> rootNode;
43
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")
47         public Proxy() {
48             // For Externalizable
49         }
50
51         Proxy(final MetadataShardDataTreeSnapshot snapshot) {
52             this.rootNode = snapshot.getRootNode().get();
53             this.metadata = snapshot.getMetadata();
54         }
55
56         @Override
57         public void writeExternal(final ObjectOutput out) throws IOException {
58             out.writeInt(metadata.size());
59             for (ShardDataTreeSnapshotMetadata<?> m : metadata.values()) {
60                 out.writeObject(m);
61             }
62
63             SerializationUtils.writeNormalizedNode(out, rootNode);
64         }
65
66         @Override
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);
70
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();
76                 if (m != null) {
77                     metaBuilder.put(m.getType(), m);
78                 } else {
79                     LOG.warn("Skipping null metadata");
80                 }
81             }
82
83             metadata = metaBuilder.build();
84             rootNode = SerializationUtils.readNormalizedNode(in).get();
85         }
86
87         private Object readResolve() {
88             return new MetadataShardDataTreeSnapshot(rootNode, metadata);
89         }
90     }
91
92     private static final long serialVersionUID = 1L;
93
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;
98
99     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "See above justification.")
100     private final NormalizedNode<?, ?> rootNode;
101
102     public MetadataShardDataTreeSnapshot(final NormalizedNode<?, ?> rootNode) {
103         this(rootNode, ImmutableMap.of());
104     }
105
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);
110     }
111
112     public Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> getMetadata() {
113         return metadata;
114     }
115
116     @Override
117     NormalizedNode<?, ?> rootNode() {
118         return rootNode;
119     }
120
121     @Override
122     PayloadVersion version() {
123         return PayloadVersion.NEON_SR2;
124     }
125
126     private Object writeReplace() {
127         return new Proxy(this);
128     }
129
130     @Override
131     public String toString() {
132         return MoreObjects.toStringHelper(this).add("metadata", metadata).toString();
133     }
134 }