4ea4202adc6a0f5e50f0bcdda641e339bb61068c
[netconf.git] / netconf / netconf-netty-util / src / main / java / org / opendaylight / netconf / nettyutil / handler / NetconfEXIToMessageDecoder.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.netconf.nettyutil.handler;
9
10 import static java.util.Objects.requireNonNull;
11
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;
35 import org.xml.sax.XMLReader;
36
37 public final class NetconfEXIToMessageDecoder extends ByteToMessageDecoder {
38
39     private static final Logger LOG = LoggerFactory.getLogger(NetconfEXIToMessageDecoder.class);
40     private static final SAXTransformerFactory FACTORY;
41
42     static {
43         final TransformerFactory f = SAXTransformerFactory.newInstance();
44         if (!f.getFeature(SAXTransformerFactory.FEATURE)) {
45             throw new TransformerFactoryConfigurationError(
46                     String.format("Factory %s is not a SAXTransformerFactory", f));
47         }
48
49         FACTORY = (SAXTransformerFactory)f;
50     }
51
52     /**
53      * This class is not marked as shared, so it can be attached to only a single channel,
54      * which means that {@link #decode(ChannelHandlerContext, ByteBuf, List)}
55      * cannot be invoked concurrently. Hence we can reuse the reader.
56      */
57     private final XMLReader reader;
58     private final DocumentBuilder documentBuilder;
59
60     private NetconfEXIToMessageDecoder(final XMLReader reader) {
61         this.reader = requireNonNull(reader);
62         this.documentBuilder = UntrustedXML.newDocumentBuilder();
63     }
64
65     public static NetconfEXIToMessageDecoder create(final NetconfEXICodec codec) throws EXIException {
66         return new NetconfEXIToMessageDecoder(codec.getReader());
67     }
68
69     @Override
70     protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out)
71             throws IOException, SAXException, TransformerConfigurationException {
72         /*
73          * Note that we could loop here and process all the messages, but we can't do that.
74          * The reason is <stop-exi> operation, which has the contract of immediately stopping
75          * the use of EXI, which means the next message needs to be decoded not by us, but rather
76          * by the XML decoder.
77          */
78
79         // If empty Byte buffer is passed to r.parse, EOFException is thrown
80         if (!in.isReadable()) {
81             LOG.debug("No more content in incoming buffer.");
82             return;
83         }
84
85         if (LOG.isTraceEnabled()) {
86             LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
87         }
88
89         final TransformerHandler handler = FACTORY.newTransformerHandler();
90         reader.setContentHandler(handler);
91
92         final DOMResult domResult = new DOMResult(documentBuilder.newDocument());
93         handler.setResult(domResult);
94
95         try (InputStream is = new ByteBufInputStream(in)) {
96             // Performs internal reset before doing anything
97             reader.parse(new InputSource(is));
98         }
99
100         out.add(new NetconfMessage((Document) domResult.getNode()));
101     }
102 }