Improve NETCONF session ID handling
[netconf.git] / protocol / netconf-server / src / main / java / org / opendaylight / netconf / server / NetconfServerSessionNegotiator.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.server;
9
10 import static java.util.Objects.requireNonNull;
11
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;
19 import java.util.Map;
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;
29
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";
34
35     private final @NonNull SessionIdType sessionId;
36
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);
44     }
45
46     @Override
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);
52     }
53
54     @Override
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",
61                 "client");
62         });
63
64         LOG.debug("Additional header from hello parsed as {} from {}", parsedHeader, additionalHeader);
65         return new NetconfServerSession(sessionListener, channel, sessionId, parsedHeader);
66     }
67
68     /**
69      * Get a name of the host.
70      *
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
74      */
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());
81         } else {
82             return Map.entry(UNKNOWN, UNKNOWN);
83         }
84     }
85 }