Added feature for sal-mdsal-xsql
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / handler / NetconfHelloMessageToXMLEncoder.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.channel.ChannelHandlerContext;
12
13 import java.io.IOException;
14
15 import javax.xml.transform.TransformerException;
16
17 import org.opendaylight.controller.netconf.api.NetconfMessage;
18 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
19 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
20
21 import com.google.common.annotations.VisibleForTesting;
22 import com.google.common.base.Charsets;
23 import com.google.common.base.Optional;
24 import com.google.common.base.Preconditions;
25
26 /**
27  * Customized NetconfMessageToXMLEncoder that serializes additional header with
28  * session metadata along with
29  * {@link org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage}
30  * . Used by netconf clients to send information about the user, ip address,
31  * protocol etc.
32  * <p/>
33  * Hello message with header example:
34  * <p/>
35  *
36  * <pre>
37  * {@code
38  * [tomas;10.0.0.0/10000;tcp;1000;1000;;/home/tomas;;]
39  * <hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
40  * <capabilities>
41  * <capability>urn:ietf:params:netconf:base:1.0</capability>
42  * </capabilities>
43  * </hello>
44  * }
45  * </pre>
46  */
47 public final class NetconfHelloMessageToXMLEncoder extends NetconfMessageToXMLEncoder {
48     @Override
49     @VisibleForTesting
50     public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out) throws IOException, TransformerException {
51         Preconditions.checkState(msg instanceof NetconfHelloMessage, "Netconf message of type %s expected, was %s",
52                 NetconfHelloMessage.class, msg.getClass());
53         Optional<NetconfHelloMessageAdditionalHeader> headerOptional = ((NetconfHelloMessage) msg)
54                 .getAdditionalHeader();
55
56         // If additional header present, serialize it along with netconf hello
57         // message
58         if (headerOptional.isPresent()) {
59             out.writeBytes(headerOptional.get().toFormattedString().getBytes(Charsets.UTF_8));
60         }
61
62         super.encode(ctx, msg, out);
63     }
64 }