2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.persisted;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
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;
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;
28 * An {@link AbstractVersionedShardDataTreeSnapshot} which contains additional metadata.
30 * @author Robert Varga
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);
39 private Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
40 private NormalizedNode<?, ?> rootNode;
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")
49 Proxy(final MetadataShardDataTreeSnapshot snapshot) {
50 this.rootNode = snapshot.getRootNode().get();
51 this.metadata = snapshot.getMetadata();
55 public void writeExternal(final ObjectOutput out) throws IOException {
56 out.writeInt(metadata.size());
57 for (ShardDataTreeSnapshotMetadata<?> m : metadata.values()) {
61 SerializationUtils.serializeNormalizedNode(rootNode, out);
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);
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();
75 metaBuilder.put(m.getType(), m);
77 LOG.warn("Skipping null metadata");
81 metadata = metaBuilder.build();
82 rootNode = Verify.verifyNotNull(SerializationUtils.deserializeNormalizedNode(in));
85 private Object readResolve() {
86 return new MetadataShardDataTreeSnapshot(rootNode, metadata);
90 private static final long serialVersionUID = 1L;
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;
97 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "See above justification.")
98 private final NormalizedNode<?, ?> rootNode;
100 public MetadataShardDataTreeSnapshot(final NormalizedNode<?, ?> rootNode) {
101 this(rootNode, ImmutableMap.of());
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);
110 public Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> getMetadata() {
115 NormalizedNode<?, ?> rootNode() {
120 PayloadVersion version() {
121 return PayloadVersion.BORON;
124 private Object writeReplace() {
125 return new Proxy(this);