/*
 * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.controller.cluster.datastore.persisted;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.Beta;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;
import java.util.Map;
import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * An {@link AbstractVersionedShardDataTreeSnapshot} which contains additional metadata.
 *
 * @author Robert Varga
 */
@Beta
public final class MetadataShardDataTreeSnapshot extends AbstractVersionedShardDataTreeSnapshot
        implements Serializable {
    private static final class Proxy implements Externalizable {
        private static final long serialVersionUID = 1L;
        private static final Logger LOG = LoggerFactory.getLogger(MetadataShardDataTreeSnapshot.class);

        private Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;
        private NormalizedNode<?, ?> rootNode;

        // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
        // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
        @SuppressWarnings("checkstyle:RedundantModifier")
        public Proxy() {
            // For Externalizable
        }

        Proxy(final MetadataShardDataTreeSnapshot snapshot) {
            this.rootNode = snapshot.getRootNode().get();
            this.metadata = snapshot.getMetadata();
        }

        @Override
        public void writeExternal(final ObjectOutput out) throws IOException {
            out.writeInt(metadata.size());
            for (ShardDataTreeSnapshotMetadata<?> m : metadata.values()) {
                out.writeObject(m);
            }

            SerializationUtils.writeNormalizedNode(out, rootNode);
        }

        @Override
        public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
            final int metaSize = in.readInt();
            checkArgument(metaSize >= 0, "Invalid negative metadata map length %s", metaSize);

            // Default pre-allocate is 4, which should be fine
            final Builder<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>>
                    metaBuilder = ImmutableMap.builder();
            for (int i = 0; i < metaSize; ++i) {
                final ShardDataTreeSnapshotMetadata<?> m = (ShardDataTreeSnapshotMetadata<?>) in.readObject();
                if (m != null) {
                    metaBuilder.put(m.getType(), m);
                } else {
                    LOG.warn("Skipping null metadata");
                }
            }

            metadata = metaBuilder.build();
            rootNode = SerializationUtils.readNormalizedNode(in).get();
        }

        private Object readResolve() {
            return new MetadataShardDataTreeSnapshot(rootNode, metadata);
        }
    }

    private static final long serialVersionUID = 1L;

    @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "This field is not Serializable but this class "
            + "implements writeReplace to delegate serialization to a Proxy class and thus instances of this class "
            + "aren't serialized. FindBugs does not recognize this.")
    private final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata;

    @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "See above justification.")
    private final NormalizedNode<?, ?> rootNode;

    public MetadataShardDataTreeSnapshot(final NormalizedNode<?, ?> rootNode) {
        this(rootNode, ImmutableMap.of());
    }

    public MetadataShardDataTreeSnapshot(final NormalizedNode<?, ?> rootNode,
            final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> metadata) {
        this.rootNode = requireNonNull(rootNode);
        this.metadata = ImmutableMap.copyOf(metadata);
    }

    public Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> getMetadata() {
        return metadata;
    }

    @Override
    NormalizedNode<?, ?> rootNode() {
        return rootNode;
    }

    @Override
    PayloadVersion version() {
        return PayloadVersion.NEON_SR2;
    }

    private Object writeReplace() {
        return new Proxy(this);
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this).add("metadata", metadata).toString();
    }
}

@@ -866,32 +866,73 @@
 o.Serializable;%0A
+import java.io.StreamCorruptedException;%0A
 import java.util
@@ -1008,34 +1008,334 @@
 .stream.
-SerializationUtils
+NormalizedNodeDataInput;%0Aimport org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;%0Aimport org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;%0Aimport org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeStreamVersion
 ;%0Aimport
@@ -2055,24 +2055,77 @@
 %3E metadata;%0A
+        private NormalizedNodeStreamVersion version;%0A
         priv
@@ -2688,24 +2688,90 @@
 Metadata();%0A
+            this.version = snapshot.version().getStreamVersion();%0A
         %7D%0A%0A 
@@ -3026,33 +3026,32 @@
 ;%0A            %7D%0A
-%0A
             Seri
@@ -3046,34 +3046,170 @@
         
-SerializationUtils
+out.writeBoolean(true);%0A            try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out, version)) %7B%0A                stream
 .writeNo
@@ -3221,21 +3221,16 @@
 zedNode(
-out, 
 rootNode
@@ -3224,32 +3224,46 @@
 Node(rootNode);%0A
+            %7D%0A
         %7D%0A%0A     
@@ -4170,64 +4170,363 @@
     
-rootNode = SerializationUtils.readNormalizedNode(in).get
+final boolean present = in.readBoolean();%0A            if (!present) %7B%0A                throw new StreamCorruptedException(%22Unexpected missing root node%22);%0A            %7D%0A%0A            final NormalizedNodeDataInput stream = NormalizedNodeInputOutput.newDataInput(in);%0A            version = stream.getVersion();%0A            rootNode = stream.readNormalizedNode
 ();%0A

Use explicit versioning in MetadataShardDataTreeSnapshot