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.checkerframework.checker.index.qual.NonNegative;
19 import org.opendaylight.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
21 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
22 import org.opendaylight.netconf.nettyutil.AbstractNetconfSessionNegotiator;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public final class NetconfServerSessionNegotiator
27 extends AbstractNetconfSessionNegotiator<NetconfServerSession, NetconfServerSessionListener> {
28 private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSessionNegotiator.class);
29 private static final String UNKNOWN = "unknown";
31 private final long sessionId;
33 NetconfServerSessionNegotiator(final NetconfHelloMessage hello, final long sessionId,
34 final Promise<NetconfServerSession> promise, final Channel channel, final Timer timer,
35 final NetconfServerSessionListener sessionListener, final long connectionTimeoutMillis,
36 final @NonNegative int maximumIncomingChunkSize) {
37 super(hello, promise, channel, timer, sessionListener, connectionTimeoutMillis,
38 maximumIncomingChunkSize);
39 this.sessionId = sessionId;
43 protected void handleMessage(final NetconfHelloMessage netconfMessage) throws NetconfDocumentedException {
44 NetconfServerSession session = getSessionForHelloMessage(netconfMessage);
45 replaceHelloMessageInboundHandler(session);
46 // Negotiation successful after all non hello messages were processed
47 negotiationSuccessful(session);
51 protected NetconfServerSession getSession(final NetconfServerSessionListener sessionListener, final Channel channel,
52 final NetconfHelloMessage message) {
53 final var additionalHeader = message.getAdditionalHeader();
54 final var parsedHeader = additionalHeader.orElseGet(() -> {
55 final var hostName = getHostName(channel.localAddress());
56 return new NetconfHelloMessageAdditionalHeader(UNKNOWN, hostName.getValue(), hostName.getKey(), "tcp",
60 LOG.debug("Additional header from hello parsed as {} from {}", parsedHeader, additionalHeader);
61 return new NetconfServerSession(sessionListener, channel, sessionId, parsedHeader);
65 * Get a name of the host.
67 * @param socketAddress type of socket address LocalAddress, or
68 * InetSocketAddress, for others returns unknown
69 * @return Two values - port and host of socket address
71 protected static Map.Entry<String, String> getHostName(final SocketAddress socketAddress) {
72 if (socketAddress instanceof InetSocketAddress inetSocketAddress) {
73 return new SimpleImmutableEntry<>(Integer.toString(inetSocketAddress.getPort()),
74 inetSocketAddress.getHostString());
75 } else if (socketAddress instanceof LocalAddress localAddress) {
76 return new SimpleImmutableEntry<>(UNKNOWN, localAddress.id());
78 return new SimpleImmutableEntry<>(UNKNOWN, UNKNOWN);