BUG-2510: Remove all registrations when a routed rpc is closed
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / handler / NetconfMessageToEXIEncoder.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.ByteBufOutputStream;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.handler.codec.MessageToByteEncoder;
15 import java.io.IOException;
16 import java.io.OutputStream;
17 import javax.xml.transform.Transformer;
18 import javax.xml.transform.TransformerException;
19 import javax.xml.transform.dom.DOMSource;
20 import javax.xml.transform.sax.SAXResult;
21 import javax.xml.transform.sax.SAXTransformerFactory;
22 import org.opendaylight.controller.netconf.api.NetconfMessage;
23 import org.openexi.proc.common.EXIOptionsException;
24 import org.openexi.sax.Transmogrifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public final class NetconfMessageToEXIEncoder extends MessageToByteEncoder<NetconfMessage> {
29
30     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageToEXIEncoder.class);
31
32     private static final SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
33     private final NetconfEXICodec codec;
34
35     public NetconfMessageToEXIEncoder(final NetconfEXICodec codec) {
36         this.codec = Preconditions.checkNotNull(codec);
37     }
38
39     @Override
40     protected void encode(final ChannelHandlerContext ctx, final NetconfMessage msg, final ByteBuf out) throws EXIOptionsException, IOException, TransformerException {
41         LOG.trace("Sent to encode : {}", msg);
42
43         try (final OutputStream os = new ByteBufOutputStream(out)) {
44             final Transmogrifier transmogrifier = codec.getTransmogrifier();
45             transmogrifier.setOutputStream(os);
46
47             final Transformer transformer = saxTransformerFactory.newTransformer();
48             transformer.transform(new DOMSource(msg.getDocument()), new SAXResult(transmogrifier.getSAXTransmogrifier()));
49         }
50     }
51 }

©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.