/*
 * Copyright (c) 2014 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.netconf.util.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;

import org.opendaylight.controller.netconf.api.NetconfDeserializerException;
import org.opendaylight.controller.netconf.api.NetconfMessage;
import org.opendaylight.controller.netconf.util.xml.XmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;

public final class NetconfXMLToMessageDecoder extends ByteToMessageDecoder {
    private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToMessageDecoder.class);

    // FIXME: this is funky way of creating arrays
    private static final List<byte[]> POSSIBLE_ENDS = ImmutableList.of(
            "]\n".getBytes(Charsets.UTF_8), "]\r\n".getBytes(Charsets.UTF_8));
    private static final List<byte[]> POSSIBLE_STARTS = ImmutableList.of(
            "[".getBytes(Charsets.UTF_8), "\r\n[".getBytes(Charsets.UTF_8), "\n[".getBytes(Charsets.UTF_8));

    @Override
    @VisibleForTesting
    public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        if (in.readableBytes() == 0) {
            LOG.debug("No more content in incoming buffer.");
            return;
        }

        in.markReaderIndex();
        try {
            LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
            byte[] bytes = new byte[in.readableBytes()];
            in.readBytes(bytes);

            logMessage(bytes);

            String additionalHeader = null;

            if (startsWithAdditionalHeader(bytes)) {
                // Auth information containing username, ip address... extracted for monitoring
                int endOfAuthHeader = getAdditionalHeaderEndIndex(bytes);
                if (endOfAuthHeader > -1) {
                    byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader + 2);
                    additionalHeader = additionalHeaderToString(additionalHeaderBytes);
                    bytes = Arrays.copyOfRange(bytes, endOfAuthHeader + 2, bytes.length);
                }
            }
            NetconfMessage message;
            try {
                Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
                message = new NetconfMessage(doc, additionalHeader);
            } catch (final SAXException | IOException | IllegalStateException e) {
                throw new NetconfDeserializerException("Could not parse message from " + new String(bytes), e);
            }

            out.add(message);
        } finally {
            in.discardReadBytes();
        }
    }

    private int getAdditionalHeaderEndIndex(byte[] bytes) {
        for (byte[] possibleEnd : POSSIBLE_ENDS) {
            int idx = findByteSequence(bytes, possibleEnd);

            if (idx != -1) {
                return idx;
            }
        }

        return -1;
    }

    private static int findByteSequence(final byte[] bytes, final byte[] sequence) {
        if (bytes.length < sequence.length) {
            throw new IllegalArgumentException("Sequence to be found is longer than the given byte array.");
        }
        if (bytes.length == sequence.length) {
            if (Arrays.equals(bytes, sequence)) {
                return 0;
            } else {
                return -1;
            }
        }
        int j = 0;
        for (int i = 0; i < bytes.length; i++) {
            if (bytes[i] == sequence[j]) {
                j++;
                if (j == sequence.length) {
                    return i - j + 1;
                }
            } else {
                j = 0;
            }
        }
        return -1;
    }

    private boolean startsWithAdditionalHeader(byte[] bytes) {
        for (byte[] possibleStart : POSSIBLE_STARTS) {
            int i = 0;
            for (byte b : possibleStart) {
                if(bytes[i] != b)
                    break;

                return true;
            }
        }

        return false;
    };

    private void logMessage(byte[] bytes) {
        String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
        LOG.debug("Parsing message \n{}", s);
    }

    private String additionalHeaderToString(byte[] bytes) {
        return Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
    }

}

@@ -1315,59 +1315,8 @@
 );%0A%0A
-    // FIXME: this is funky way of creating arrays%0A
     
@@ -1399,72 +1399,76 @@
     
-%22%5D%5Cn%22.getBytes(Charsets.UTF_8), %22%5D%5Cr%5Cn%22.getBytes(Charsets.UTF_8)
+new byte%5B%5D %7B '%5D', '%5Cn' %7D,%0A            new byte%5B%5D %7B '%5D', '%5Cr', '%5Cn' %7D
 );%0A 
@@ -1556,102 +1556,108 @@
     
-%22%5B%22.getBytes(Charsets.UTF_8), %22%5Cr%5Cn%5B%22.getBytes(Charsets.UTF_8), %22%5Cn%5B%22.getBytes(Charsets.UTF_8)
+new byte%5B%5D %7B '%5B' %7D,%0A            new byte%5B%5D %7B '%5Cr', '%5Cn', '%5B' %7D,%0A            new byte%5B%5D %7B '%5Cn', '%5B' %7D
 );%0A%0A
@@ -2210,16 +2210,358 @@
  null;%0A%0A
+            // FIXME: this has to be moved into the negotiator and explained as to what the heck%0A            // is going on. This is definitely not specified in NETCONF and has no place here. It%0A            // requires reading all data and incurs inefficiency by being unable to pipe the ByteBuf%0A            // directly into the XML decoder.%0A
         

Rewrite POSSIBLE_ENDS and POSSIBLE_STARTS