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.server;
10 import static java.util.Objects.requireNonNull;
12 import io.netty.channel.Channel;
13 import io.netty.channel.local.LocalAddress;
14 import io.netty.util.Timer;
15 import io.netty.util.concurrent.Promise;
16 import java.net.InetSocketAddress;
17 import java.net.SocketAddress;
18 import java.util.AbstractMap.SimpleImmutableEntry;
20 import org.checkerframework.checker.index.qual.NonNegative;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.netconf.api.NetconfDocumentedException;
23 import org.opendaylight.netconf.api.messages.HelloMessage;
24 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
25 import org.opendaylight.netconf.nettyutil.AbstractNetconfSessionNegotiator;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public final class NetconfServerSessionNegotiator
31 extends AbstractNetconfSessionNegotiator<NetconfServerSession, NetconfServerSessionListener> {
32 private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSessionNegotiator.class);
33 private static final String UNKNOWN = "unknown";
35 private final @NonNull SessionIdType sessionId;
37 NetconfServerSessionNegotiator(final HelloMessage hello, final SessionIdType sessionId,
38 final Promise<NetconfServerSession> promise, final Channel channel, final Timer timer,
39 final NetconfServerSessionListener sessionListener, final long connectionTimeoutMillis,
40 final @NonNegative int maximumIncomingChunkSize) {
41 super(hello, promise, channel, timer, sessionListener, connectionTimeoutMillis,
42 maximumIncomingChunkSize);
43 this.sessionId = requireNonNull(sessionId);
47 protected void handleMessage(final HelloMessage netconfMessage) throws NetconfDocumentedException {
48 NetconfServerSession session = getSessionForHelloMessage(netconfMessage);
49 replaceHelloMessageInboundHandler(session);
50 // Negotiation successful after all non hello messages were processed
51 negotiationSuccessful(session);
55 protected NetconfServerSession getSession(final NetconfServerSessionListener sessionListener, final Channel channel,
56 final HelloMessage message) {
57 final var additionalHeader = message.getAdditionalHeader();
58 final var parsedHeader = additionalHeader.orElseGet(() -> {
59 final var hostName = getHostName(channel.localAddress());
60 return new NetconfHelloMessageAdditionalHeader(UNKNOWN, hostName.getValue(), hostName.getKey(), "tcp",
64 LOG.debug("Additional header from hello parsed as {} from {}", parsedHeader, additionalHeader);
65 return new NetconfServerSession(sessionListener, channel, sessionId, parsedHeader);
69 * Get a name of the host.
71 * @param socketAddress type of socket address LocalAddress, or
72 * InetSocketAddress, for others returns unknown
73 * @return Two values - port and host of socket address
75 protected static Map.Entry<String, String> getHostName(final SocketAddress socketAddress) {
76 if (socketAddress instanceof InetSocketAddress inetSocketAddress) {
77 return new SimpleImmutableEntry<>(Integer.toString(inetSocketAddress.getPort()),
78 inetSocketAddress.getHostString());
79 } else if (socketAddress instanceof LocalAddress localAddress) {
80 return Map.entry(UNKNOWN, localAddress.id());
82 return Map.entry(UNKNOWN, UNKNOWN);