Merge "Fixed for bug 1168 : Issue while update subnet"
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / handler / NetconfMessageToXMLEncoder.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 io.netty.buffer.ByteBuf;
11 import io.netty.buffer.ByteBufOutputStream;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.handler.codec.MessageToByteEncoder;
14
15 import java.io.BufferedWriter;
16 import java.io.IOException;
17 import java.io.OutputStream;
18 import java.io.OutputStreamWriter;
19
20 import javax.xml.transform.OutputKeys;
21 import javax.xml.transform.Transformer;
22 import javax.xml.transform.TransformerException;
23 import javax.xml.transform.TransformerFactory;
24 import javax.xml.transform.dom.DOMSource;
25 import javax.xml.transform.stream.StreamResult;
26
27 import org.opendaylight.controller.netconf.api.NetconfMessage;
28 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Comment;
32
33 import com.google.common.annotations.VisibleForTesting;
34 import com.google.common.base.Optional;
35
36 public class NetconfMessageToXMLEncoder extends MessageToByteEncoder<NetconfMessage> {
37     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageToXMLEncoder.class);
38     private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
39
40     private final Optional<String> clientId;
41
42     public NetconfMessageToXMLEncoder() {
43         this(Optional.<String>absent());
44     }
45
46     public NetconfMessageToXMLEncoder(Optional<String> clientId) {
47         this.clientId = clientId;
48     }
49
50     @Override
51     @VisibleForTesting
52     public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out) throws IOException, TransformerException {
53         LOG.trace("Sent to encode : {}", XmlUtil.toString(msg.getDocument()));
54
55         if (clientId.isPresent()) {
56             Comment comment = msg.getDocument().createComment("clientId:" + clientId.get());
57             msg.getDocument().appendChild(comment);
58         }
59
60         try (OutputStream os = new ByteBufOutputStream(out)) {
61             Transformer transformer = FACTORY.newTransformer();
62             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
63             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
64
65             // Wrap OutputStreamWriter with BufferedWriter as suggested in javadoc for OutputStreamWriter
66             StreamResult result = new StreamResult(new BufferedWriter(new OutputStreamWriter(os)));
67             DOMSource source = new DOMSource(msg.getDocument());
68             transformer.transform(source, result);
69         }
70     }
71 }