6d2508757a4e3138c5f3effe1cb7a2e08c757dd3
[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 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;
35
36 public final class NetconfEXIToMessageDecoder extends ByteToMessageDecoder {
37
38     private static final Logger LOG = LoggerFactory.getLogger(NetconfEXIToMessageDecoder.class);
39     private static final SAXTransformerFactory FACTORY;
40     static {
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));
44         }
45
46         FACTORY = (SAXTransformerFactory)f;
47     }
48
49     /**
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.
53      */
54     private final EXIReader reader;
55     private final DocumentBuilder documentBuilder;
56
57     private NetconfEXIToMessageDecoder(final EXIReader reader) {
58         this.reader = Preconditions.checkNotNull(reader);
59         this.documentBuilder = UntrustedXML.newDocumentBuilder();
60     }
61
62     public static NetconfEXIToMessageDecoder create(final NetconfEXICodec codec) throws EXIOptionsException {
63         return new NetconfEXIToMessageDecoder(codec.getReader());
64     }
65
66     @Override
67     protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws EXIOptionsException, IOException, SAXException, TransformerConfigurationException  {
68         /*
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
72          * by the XML decoder.
73          */
74
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.");
78             return;
79         }
80
81         if (LOG.isTraceEnabled()) {
82             LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
83         }
84
85         final TransformerHandler handler = FACTORY.newTransformerHandler();
86         reader.setContentHandler(handler);
87
88         final DOMResult domResult = new DOMResult(documentBuilder.newDocument());
89         handler.setResult(domResult);
90
91         try (final InputStream is = new ByteBufInputStream(in)) {
92             // Performs internal reset before doing anything
93             reader.parse(new InputSource(is));
94         }
95
96         out.add(new NetconfMessage((Document) domResult.getNode()));
97     }
98 }