2 * Copyright (c) 2014 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.netconf.nettyutil.handler;
10 import static java.util.Objects.requireNonNull;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.ByteBufInputStream;
14 import io.netty.buffer.ByteBufUtil;
15 import io.netty.channel.ChannelHandlerContext;
16 import io.netty.handler.codec.ByteToMessageDecoder;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.util.List;
20 import javax.xml.parsers.DocumentBuilder;
21 import javax.xml.transform.TransformerConfigurationException;
22 import javax.xml.transform.TransformerFactory;
23 import javax.xml.transform.TransformerFactoryConfigurationError;
24 import javax.xml.transform.dom.DOMResult;
25 import javax.xml.transform.sax.SAXTransformerFactory;
26 import javax.xml.transform.sax.TransformerHandler;
27 import org.opendaylight.netconf.api.NetconfMessage;
28 import org.opendaylight.netconf.shaded.exificient.core.exceptions.EXIException;
29 import org.opendaylight.yangtools.util.xml.UntrustedXML;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.w3c.dom.Document;
33 import org.xml.sax.InputSource;
34 import org.xml.sax.SAXException;
36 public final class NetconfEXIToMessageDecoder extends ByteToMessageDecoder {
38 private static final Logger LOG = LoggerFactory.getLogger(NetconfEXIToMessageDecoder.class);
39 private static final SAXTransformerFactory FACTORY;
42 final TransformerFactory f = SAXTransformerFactory.newInstance();
43 if (!f.getFeature(SAXTransformerFactory.FEATURE)) {
44 throw new TransformerFactoryConfigurationError(
45 String.format("Factory %s is not a SAXTransformerFactory", f));
48 FACTORY = (SAXTransformerFactory)f;
52 * This class is not marked as shared, so it can be attached to only a single channel,
53 * which means that {@link #decode(ChannelHandlerContext, ByteBuf, List)}
54 * cannot be invoked concurrently. Hence we can reuse the reader.
56 private final ThreadLocalSAXDecoder reader;
57 private final DocumentBuilder documentBuilder;
59 private NetconfEXIToMessageDecoder(final ThreadLocalSAXDecoder reader) {
60 this.reader = requireNonNull(reader);
61 this.documentBuilder = UntrustedXML.newDocumentBuilder();
64 public static NetconfEXIToMessageDecoder create(final NetconfEXICodec codec) throws EXIException {
65 return new NetconfEXIToMessageDecoder(codec.getReader());
69 protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
70 throws IOException, SAXException, TransformerConfigurationException {
72 * Note that we could loop here and process all the messages, but we can't do that.
73 * The reason is <stop-exi> operation, which has the contract of immediately stopping
74 * the use of EXI, which means the next message needs to be decoded not by us, but rather
78 // If empty Byte buffer is passed to r.parse, EOFException is thrown
79 if (!in.isReadable()) {
80 LOG.debug("No more content in incoming buffer.");
84 if (LOG.isTraceEnabled()) {
85 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
88 final TransformerHandler handler = FACTORY.newTransformerHandler();
89 reader.setContentHandler(handler);
91 final DOMResult domResult = new DOMResult(documentBuilder.newDocument());
92 handler.setResult(domResult);
94 try (InputStream is = new ByteBufInputStream(in)) {
95 // Performs internal reset before doing anything
96 reader.parse(new InputSource(is));
99 out.add(new NetconfMessage((Document) domResult.getNode()));