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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import io.netty.channel.Channel;
12 import io.netty.channel.local.LocalAddress;
13 import io.netty.util.Timer;
14 import io.netty.util.concurrent.Promise;
15 import java.net.InetSocketAddress;
16 import java.net.SocketAddress;
17 import java.util.AbstractMap.SimpleImmutableEntry;
19 import org.opendaylight.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.netconf.api.NetconfServerSessionPreferences;
21 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
22 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
23 import org.opendaylight.netconf.nettyutil.AbstractNetconfSessionNegotiator;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public final class NetconfServerSessionNegotiator
28 extends AbstractNetconfSessionNegotiator<NetconfServerSessionPreferences, NetconfServerSession,
29 NetconfServerSessionListener> {
30 private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSessionNegotiator.class);
31 private static final String UNKNOWN = "unknown";
33 protected NetconfServerSessionNegotiator(final NetconfServerSessionPreferences sessionPreferences,
34 final Promise<NetconfServerSession> promise, final Channel channel, final Timer timer,
35 final NetconfServerSessionListener sessionListener, final long connectionTimeoutMillis) {
36 super(sessionPreferences, promise, channel, timer, sessionListener, connectionTimeoutMillis);
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 @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE",
49 justification = "SpotBugs does not grok generic return of getSessionPreferences()")
50 protected NetconfServerSession getSession(final NetconfServerSessionListener sessionListener, final Channel channel,
51 final NetconfHelloMessage message) {
52 final var additionalHeader = message.getAdditionalHeader();
53 final var parsedHeader = additionalHeader.orElseGet(() -> {
54 final var hostName = getHostName(channel.localAddress());
55 return new NetconfHelloMessageAdditionalHeader(UNKNOWN, hostName.getValue(), hostName.getKey(), "tcp",
59 LOG.debug("Additional header from hello parsed as {} from {}", parsedHeader, additionalHeader);
60 return new NetconfServerSession(sessionListener, channel, getSessionPreferences().getSessionId(), parsedHeader);
64 * Get a name of the host.
66 * @param socketAddress type of socket address LocalAddress, or
67 * InetSocketAddress, for others returns unknown
68 * @return Two values - port and host of socket address
70 protected static Map.Entry<String, String> getHostName(final SocketAddress socketAddress) {
71 if (socketAddress instanceof InetSocketAddress) {
72 final var inetSocketAddress = (InetSocketAddress) socketAddress;
73 return new SimpleImmutableEntry<>(Integer.toString(inetSocketAddress.getPort()),
74 inetSocketAddress.getHostString());
75 } else if (socketAddress instanceof LocalAddress) {
76 return new SimpleImmutableEntry<>(UNKNOWN, ((LocalAddress) socketAddress).id());
78 return new SimpleImmutableEntry<>(UNKNOWN, UNKNOWN);