2 * Copyright (c) 2013 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.netconf.impl;
10 import io.netty.channel.Channel;
11 import io.netty.channel.local.LocalAddress;
12 import io.netty.util.Timer;
13 import io.netty.util.concurrent.Promise;
14 import java.net.InetSocketAddress;
15 import java.net.SocketAddress;
16 import java.util.AbstractMap.SimpleImmutableEntry;
18 import org.opendaylight.netconf.api.NetconfDocumentedException;
19 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
20 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
21 import org.opendaylight.netconf.nettyutil.AbstractNetconfSessionNegotiator;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public final class NetconfServerSessionNegotiator
26 extends AbstractNetconfSessionNegotiator<NetconfServerSession, NetconfServerSessionListener> {
27 private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSessionNegotiator.class);
28 private static final String UNKNOWN = "unknown";
30 private final long sessionId;
32 NetconfServerSessionNegotiator(final NetconfHelloMessage hello, final long sessionId,
33 final Promise<NetconfServerSession> promise, final Channel channel, final Timer timer,
34 final NetconfServerSessionListener sessionListener, final long connectionTimeoutMillis) {
35 super(hello, promise, channel, timer, sessionListener, connectionTimeoutMillis);
36 this.sessionId = sessionId;
40 protected void handleMessage(final NetconfHelloMessage netconfMessage) throws NetconfDocumentedException {
41 NetconfServerSession session = getSessionForHelloMessage(netconfMessage);
42 replaceHelloMessageInboundHandler(session);
43 // Negotiation successful after all non hello messages were processed
44 negotiationSuccessful(session);
48 protected NetconfServerSession getSession(final NetconfServerSessionListener sessionListener, final Channel channel,
49 final NetconfHelloMessage message) {
50 final var additionalHeader = message.getAdditionalHeader();
51 final var parsedHeader = additionalHeader.orElseGet(() -> {
52 final var hostName = getHostName(channel.localAddress());
53 return new NetconfHelloMessageAdditionalHeader(UNKNOWN, hostName.getValue(), hostName.getKey(), "tcp",
57 LOG.debug("Additional header from hello parsed as {} from {}", parsedHeader, additionalHeader);
58 return new NetconfServerSession(sessionListener, channel, sessionId, parsedHeader);
62 * Get a name of the host.
64 * @param socketAddress type of socket address LocalAddress, or
65 * InetSocketAddress, for others returns unknown
66 * @return Two values - port and host of socket address
68 protected static Map.Entry<String, String> getHostName(final SocketAddress socketAddress) {
69 if (socketAddress instanceof InetSocketAddress inetSocketAddress) {
70 return new SimpleImmutableEntry<>(Integer.toString(inetSocketAddress.getPort()),
71 inetSocketAddress.getHostString());
72 } else if (socketAddress instanceof LocalAddress localAddress) {
73 return new SimpleImmutableEntry<>(UNKNOWN, localAddress.id());
75 return new SimpleImmutableEntry<>(UNKNOWN, UNKNOWN);