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 com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.ByteBufInputStream;
13 import io.netty.buffer.ByteBufUtil;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.handler.codec.ByteToMessageDecoder;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.util.List;
19 import javax.xml.parsers.DocumentBuilder;
20 import javax.xml.transform.TransformerConfigurationException;
21 import javax.xml.transform.TransformerFactory;
22 import javax.xml.transform.TransformerFactoryConfigurationError;
23 import javax.xml.transform.dom.DOMResult;
24 import javax.xml.transform.sax.SAXTransformerFactory;
25 import javax.xml.transform.sax.TransformerHandler;
26 import org.opendaylight.netconf.api.NetconfMessage;
27 import org.opendaylight.yangtools.util.xml.UntrustedXML;
28 import org.openexi.proc.common.EXIOptionsException;
29 import org.openexi.sax.EXIReader;
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;
41 final TransformerFactory f = SAXTransformerFactory.newInstance();
42 if (!f.getFeature(SAXTransformerFactory.FEATURE)) {
43 throw new TransformerFactoryConfigurationError(String.format("Factory %s is not a SAXTransformerFactory", f));
46 FACTORY = (SAXTransformerFactory)f;
50 * This class is not marked as shared, so it can be attached to only a single channel,
51 * which means that {@link #decode(ChannelHandlerContext, ByteBuf, List)}
52 * cannot be invoked concurrently. Hence we can reuse the reader.
54 private final EXIReader reader;
55 private final DocumentBuilder documentBuilder;
57 private NetconfEXIToMessageDecoder(final EXIReader reader) {
58 this.reader = Preconditions.checkNotNull(reader);
59 this.documentBuilder = UntrustedXML.newDocumentBuilder();
62 public static NetconfEXIToMessageDecoder create(final NetconfEXICodec codec) throws EXIOptionsException {
63 return new NetconfEXIToMessageDecoder(codec.getReader());
67 protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws EXIOptionsException, IOException, SAXException, TransformerConfigurationException {
69 * Note that we could loop here and process all the messages, but we can't do that.
70 * The reason is <stop-exi> operation, which has the contract of immediately stopping
71 * the use of EXI, which means the next message needs to be decoded not by us, but rather
75 // If empty Byte buffer is passed to r.parse, EOFException is thrown
76 if (!in.isReadable()) {
77 LOG.debug("No more content in incoming buffer.");
81 if (LOG.isTraceEnabled()) {
82 LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
85 final TransformerHandler handler = FACTORY.newTransformerHandler();
86 reader.setContentHandler(handler);
88 final DOMResult domResult = new DOMResult(documentBuilder.newDocument());
89 handler.setResult(domResult);
91 try (final InputStream is = new ByteBufInputStream(in)) {
92 // Performs internal reset before doing anything
93 reader.parse(new InputSource(is));
96 out.add(new NetconfMessage((Document) domResult.getNode()));