BUG-848 Fix netconf communication while using CHUNK encoding
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / handler / NetconfXMLToMessageDecoder.java
1 /*
2  * Copyright (c) 2014 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.netconf.util.handler;
9
10 import java.util.List;
11
12 import org.opendaylight.controller.netconf.api.NetconfMessage;
13 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 import com.google.common.annotations.VisibleForTesting;
18
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.ByteBufInputStream;
21 import io.netty.buffer.ByteBufUtil;
22 import io.netty.channel.ChannelHandlerContext;
23 import io.netty.handler.codec.ByteToMessageDecoder;
24
25 public final class NetconfXMLToMessageDecoder extends ByteToMessageDecoder {
26     private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToMessageDecoder.class);
27
28     @Override
29     @VisibleForTesting
30     public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
31
32         if (in.readableBytes() != 0) {
33             LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
34             out.add(new NetconfMessage(XmlUtil.readXmlToDocument(new ByteBufInputStream(in))));
35         } else {
36             LOG.debug("No more content in incoming buffer.");
37         }
38     }
39 }