Search in sources :

Example 1 with FailedNetconfMessage

use of org.opendaylight.netconf.api.FailedNetconfMessage in project netconf by opendaylight.

the class NetconfXMLToMessageDecoder method decode.

@Override
public void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws IOException, SAXException {
    if (in.isReadable()) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
        }
        // Skip all leading whitespaces by moving the reader index to the first non whitespace character
        while (in.isReadable()) {
            if (!isWhitespace(in.readByte())) {
                // return reader index to the first non whitespace character
                in.readerIndex(in.readerIndex() - 1);
                break;
            }
        }
        // Warn about leading whitespaces
        if (in.readerIndex() != 0 && LOG.isWarnEnabled()) {
            final byte[] strippedBytes = new byte[in.readerIndex()];
            in.getBytes(0, strippedBytes, 0, in.readerIndex());
            LOG.warn("XML message with unwanted leading bytes detected. Discarded the {} leading byte(s): '{}'", in.readerIndex(), ByteBufUtil.hexDump(Unpooled.wrappedBuffer(strippedBytes)));
        }
    }
    if (in.isReadable()) {
        NetconfMessage msg;
        try {
            msg = new NetconfMessage(XmlUtil.readXmlToDocument(new ByteBufInputStream(in)));
        } catch (SAXParseException exception) {
            LOG.error("Failed to parse received message", exception);
            msg = new FailedNetconfMessage(exception);
        }
        out.add(msg);
    } else {
        LOG.debug("No more content in incoming buffer.");
    }
}
Also used : FailedNetconfMessage(org.opendaylight.netconf.api.FailedNetconfMessage) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) FailedNetconfMessage(org.opendaylight.netconf.api.FailedNetconfMessage) SAXParseException(org.xml.sax.SAXParseException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream)

Aggregations

ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 FailedNetconfMessage (org.opendaylight.netconf.api.FailedNetconfMessage)1 NetconfMessage (org.opendaylight.netconf.api.NetconfMessage)1 SAXParseException (org.xml.sax.SAXParseException)1