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