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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.