d21e1987d4582bf52fcc225498259b2046867a4f
[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.NormalizedNodeDataOutput;
28 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * An {@link AbstractVersionedShardDataTreeSnapshot} which contains additional metadata.
34  *
35  * @author Robert Varga
36  */
37 @Beta
38 public final class MetadataShardDataTreeSnapshot extends AbstractVersionedShardDataTreeSnapshot
39         implements Serializable {
40     private static final class Proxy implements Externalizable {
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         Proxy(final MetadataShardDataTreeSnapshot snapshot) {
56             this.rootNode = snapshot.getRootNode().get();
57             this.metadata = snapshot.getMetadata();
58             this.version = snapshot.version().getStreamVersion();
59         }
60
61         @Override
62         public void writeExternal(final ObjectOutput out) throws IOException {
63             out.writeInt(metadata.size());
64             for (ShardDataTreeSnapshotMetadata<?> m : metadata.values()) {
65                 out.writeObject(m);
66             }
67             out.writeBoolean(true);
68             try (NormalizedNodeDataOutput stream = version.newDataOutput(out)) {
69                 stream.writeNormalizedNode(rootNode);
70             }
71         }
72
73         @Override
74         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
75             final int metaSize = in.readInt();
76             checkArgument(metaSize >= 0, "Invalid negative metadata map length %s", metaSize);
77
78             // Default pre-allocate is 4, which should be fine
79             final Builder<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>>
80                     metaBuilder = ImmutableMap.builder();
81             for (int i = 0; i < metaSize; ++i) {
82                 final ShardDataTreeSnapshotMetadata<?> m = (ShardDataTreeSnapshotMetadata<?>) in.readObject();
83                 if (m != null) {
84                     metaBuilder.put(m.getType(), m);
85                 } else {
86                     LOG.warn("Skipping null metadata");
87                 }
88             }
89
90             metadata = metaBuilder.build();
91             final boolean present = in.readBoolean();
92             if (!present) {
93                 throw new StreamCorruptedException("Unexpected missing root node");
94             }
95
96             final NormalizedNodeDataInput stream = NormalizedNodeDataInput.newDataInput(in);
97             version = stream.getVersion();
98             rootNode = stream.readNormalizedNode();
99         }
100
101         private Object readResolve() {
102             return new MetadataShardDataTreeSnapshot(rootNode, metadata);
103         }
104     }
105
106     private static final long serialVersionUID = 1L;
107
108     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
109             + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
110             + "aren't serialized. FindBugs does not recognize this.")
111     private final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
112
113     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "See above justification.")
114     private final NormalizedNode<?, ?> rootNode;
115
116     public MetadataShardDataTreeSnapshot(final NormalizedNode<?, ?> rootNode) {
117         this(rootNode, ImmutableMap.of());
118     }
119
120     public MetadataShardDataTreeSnapshot(final NormalizedNode<?, ?> rootNode,
121             final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata) {
122         this.rootNode = requireNonNull(rootNode);
123         this.metadata = ImmutableMap.copyOf(metadata);
124     }
125
126     public Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> getMetadata() {
127         return metadata;
128     }
129
130     @Override
131     NormalizedNode<?, ?> rootNode() {
132         return rootNode;
133     }
134
135     @Override
136     PayloadVersion version() {
137         return PayloadVersion.MAGNESIUM;
138     }
139
140     private Object writeReplace() {
141         return new Proxy(this);
142     }
143
144     @Override
145     public String toString() {
146         return MoreObjects.toStringHelper(this).add("metadata", metadata).toString();
147     }
148 }