Merge "Bug 615: Removed xtend from Topology Manager"
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / 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.util.handler;
9
10 import java.io.InputStream;
11 import java.util.List;
12
13 import javax.xml.transform.TransformerFactory;
14 import javax.xml.transform.dom.DOMResult;
15 import javax.xml.transform.sax.SAXTransformerFactory;
16 import javax.xml.transform.sax.TransformerHandler;
17
18 import org.opendaylight.controller.netconf.api.NetconfMessage;
19 import org.openexi.sax.EXIReader;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Document;
23 import org.xml.sax.InputSource;
24
25 import com.google.common.base.Preconditions;
26
27 import io.netty.buffer.ByteBuf;
28 import io.netty.buffer.ByteBufInputStream;
29 import io.netty.buffer.ByteBufUtil;
30 import io.netty.channel.ChannelHandlerContext;
31 import io.netty.handler.codec.ByteToMessageDecoder;
32
33 public final class NetconfEXIToMessageDecoder extends ByteToMessageDecoder {
34
35     private static final Logger LOG = LoggerFactory.getLogger(NetconfEXIToMessageDecoder.class);
36
37 //    private static final SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
38
39     private final NetconfEXICodec codec;
40
41     public NetconfEXIToMessageDecoder(final NetconfEXICodec codec) {
42         this.codec = Preconditions.checkNotNull(codec);
43     }
44
45     @Override
46     protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws Exception {
47         /*
48          * Note that we could loop here and process all the messages, but we can't do that.
49          * The reason is <stop-exi> operation, which has the contract of immediately stopping
50          * the use of EXI, which means the next message needs to be decoded not by us, but rather
51          * by the XML decoder.
52          */
53         // If empty Byte buffer is passed to r.parse, EOFException is thrown
54
55         if (in.readableBytes() == 0) {
56             LOG.debug("No more content in incoming buffer.");
57             return;
58         }
59
60         LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
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
73         try (final InputStream is = new ByteBufInputStream(in)) {
74             r.parse(new InputSource(is));
75         }
76
77         out.add(new NetconfMessage((Document) domResult.getNode()));
78     }
79 }