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