Remove netconf from commons/opendaylight pom
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / 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.controller.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.transform.TransformerConfigurationException;
20 import javax.xml.transform.dom.DOMResult;
21 import javax.xml.transform.sax.SAXTransformerFactory;
22 import javax.xml.transform.sax.TransformerHandler;
23 import org.opendaylight.controller.netconf.api.NetconfMessage;
24 import org.openexi.proc.common.EXIOptionsException;
25 import org.openexi.sax.EXIReader;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.w3c.dom.Document;
29 import org.xml.sax.InputSource;
30 import org.xml.sax.SAXException;
31
32 public final class NetconfEXIToMessageDecoder extends ByteToMessageDecoder {
33
34     private static final Logger LOG = LoggerFactory.getLogger(NetconfEXIToMessageDecoder.class);
35     private static final SAXTransformerFactory FACTORY = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
36     /**
37      * This class is not marked as shared, so it can be attached to only a single channel,
38      * which means that {@link #decode(ChannelHandlerContext, ByteBuf, List)}
39      * cannot be invoked concurrently. Hence we can reuse the reader.
40      */
41     private final EXIReader reader;
42
43     private NetconfEXIToMessageDecoder(final EXIReader reader) {
44         this.reader = Preconditions.checkNotNull(reader);
45     }
46
47     public static NetconfEXIToMessageDecoder create(final NetconfEXICodec codec) throws EXIOptionsException {
48         return new NetconfEXIToMessageDecoder(codec.getReader());
49     }
50
51     @Override
52     protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws EXIOptionsException, IOException, SAXException, TransformerConfigurationException  {
53         /*
54          * Note that we could loop here and process all the messages, but we can't do that.
55          * The reason is <stop-exi> operation, which has the contract of immediately stopping
56          * the use of EXI, which means the next message needs to be decoded not by us, but rather
57          * by the XML decoder.
58          */
59
60         // If empty Byte buffer is passed to r.parse, EOFException is thrown
61         if (!in.isReadable()) {
62             LOG.debug("No more content in incoming buffer.");
63             return;
64         }
65
66         if (LOG.isTraceEnabled()) {
67             LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
68         }
69
70         final TransformerHandler handler = FACTORY.newTransformerHandler();
71         reader.setContentHandler(handler);
72
73         final DOMResult domResult = new DOMResult();
74         handler.setResult(domResult);
75
76         try (final InputStream is = new ByteBufInputStream(in)) {
77             // Performs internal reset before doing anything
78             reader.parse(new InputSource(is));
79         }
80
81         out.add(new NetconfMessage((Document) domResult.getNode()));
82     }
83 }