2bb0ec31563bddb7497eb0e9e3dc7eea74157598
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / netconf / impl / 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.impl;
9
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;
17 import java.util.Map;
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;
24
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";
29
30     private final long sessionId;
31
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         this.sessionId = sessionId;
37     }
38
39     @Override
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);
45     }
46
47     @Override
48     protected NetconfServerSession getSession(final NetconfServerSessionListener sessionListener, final Channel channel,
49             final NetconfHelloMessage message) {
50         final var additionalHeader = message.getAdditionalHeader();
51         final var parsedHeader = additionalHeader.orElseGet(() -> {
52             final var hostName = getHostName(channel.localAddress());
53             return new NetconfHelloMessageAdditionalHeader(UNKNOWN, hostName.getValue(), hostName.getKey(), "tcp",
54                 "client");
55         });
56
57         LOG.debug("Additional header from hello parsed as {} from {}", parsedHeader, additionalHeader);
58         return new NetconfServerSession(sessionListener, channel, sessionId, parsedHeader);
59     }
60
61     /**
62      * Get a name of the host.
63      *
64      * @param socketAddress type of socket address LocalAddress, or
65      *                      InetSocketAddress, for others returns unknown
66      * @return Two values - port and host of socket address
67      */
68     protected static Map.Entry<String, String> getHostName(final SocketAddress socketAddress) {
69         if (socketAddress instanceof InetSocketAddress inetSocketAddress) {
70             return new SimpleImmutableEntry<>(Integer.toString(inetSocketAddress.getPort()),
71                     inetSocketAddress.getHostString());
72         } else if (socketAddress instanceof LocalAddress localAddress) {
73             return new SimpleImmutableEntry<>(UNKNOWN, localAddress.id());
74         } else {
75             return new SimpleImmutableEntry<>(UNKNOWN, UNKNOWN);
76         }
77     }
78 }