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 DEFAULT_MAXIMUM_INCOMING_CHUNK_SIZE);
37 this.sessionId = sessionId;
41 protected void handleMessage(final NetconfHelloMessage netconfMessage) throws NetconfDocumentedException {
42 NetconfServerSession session = getSessionForHelloMessage(netconfMessage);
43 replaceHelloMessageInboundHandler(session);
44 // Negotiation successful after all non hello messages were processed
45 negotiationSuccessful(session);
49 protected NetconfServerSession getSession(final NetconfServerSessionListener sessionListener, final Channel channel,
50 final NetconfHelloMessage message) {
51 final var additionalHeader = message.getAdditionalHeader();
52 final var parsedHeader = additionalHeader.orElseGet(() -> {
53 final var hostName = getHostName(channel.localAddress());
54 return new NetconfHelloMessageAdditionalHeader(UNKNOWN, hostName.getValue(), hostName.getKey(), "tcp",
58 LOG.debug("Additional header from hello parsed as {} from {}", parsedHeader, additionalHeader);
59 return new NetconfServerSession(sessionListener, channel, sessionId, parsedHeader);
63 * Get a name of the host.
65 * @param socketAddress type of socket address LocalAddress, or
66 * InetSocketAddress, for others returns unknown
67 * @return Two values - port and host of socket address
69 protected static Map.Entry<String, String> getHostName(final SocketAddress socketAddress) {
70 if (socketAddress instanceof InetSocketAddress inetSocketAddress) {
71 return new SimpleImmutableEntry<>(Integer.toString(inetSocketAddress.getPort()),
72 inetSocketAddress.getHostString());
73 } else if (socketAddress instanceof LocalAddress localAddress) {
74 return new SimpleImmutableEntry<>(UNKNOWN, localAddress.id());
76 return new SimpleImmutableEntry<>(UNKNOWN, UNKNOWN);