2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.netconf.nettyutil.handler;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Charsets;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.channel.ChannelHandlerContext;
16 import java.io.IOException;
17 import javax.xml.transform.TransformerException;
18 import org.opendaylight.controller.netconf.api.NetconfMessage;
19 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
20 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
23 * Customized NetconfMessageToXMLEncoder that serializes additional header with
24 * session metadata along with
25 * {@link org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage}
26 * . Used by netconf clients to send information about the user, ip address,
29 * Hello message with header example:
34 * [tomas;10.0.0.0/10000;tcp;1000;1000;;/home/tomas;;]
35 * <hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
37 * <capability>urn:ietf:params:netconf:base:1.0</capability>
43 public final class NetconfHelloMessageToXMLEncoder extends NetconfMessageToXMLEncoder {
46 public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out) throws IOException, TransformerException {
47 Preconditions.checkState(msg instanceof NetconfHelloMessage, "Netconf message of type %s expected, was %s",
48 NetconfHelloMessage.class, msg.getClass());
49 Optional<NetconfHelloMessageAdditionalHeader> headerOptional = ((NetconfHelloMessage) msg)
50 .getAdditionalHeader();
52 // If additional header present, serialize it along with netconf hello message
53 if (headerOptional.isPresent()) {
54 out.writeBytes(headerOptional.get().toFormattedString().getBytes(Charsets.UTF_8));
57 super.encode(ctx, msg, out);