071c75f51c2d9954ab0094baa74be41961b40c3e
[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.NetconfServerSessionPreferences;
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;
25
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";
30
31     private final long sessionId;
32
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.getHelloMessage(), promise, channel, timer, sessionListener, connectionTimeoutMillis);
37         sessionId = sessionPreferences.getSessionId();
38     }
39
40     @Override
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);
46     }
47
48     @Override
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",
55                 "client");
56         });
57
58         LOG.debug("Additional header from hello parsed as {} from {}", parsedHeader, additionalHeader);
59         return new NetconfServerSession(sessionListener, channel, sessionId, parsedHeader);
60     }
61
62     /**
63      * Get a name of the host.
64      *
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
68      */
69     protected static Map.Entry<String, String> getHostName(final SocketAddress socketAddress) {
70         if (socketAddress instanceof InetSocketAddress) {
71             final var inetSocketAddress = (InetSocketAddress) socketAddress;
72             return new SimpleImmutableEntry<>(Integer.toString(inetSocketAddress.getPort()),
73                     inetSocketAddress.getHostString());
74         } else if (socketAddress instanceof LocalAddress) {
75             return new SimpleImmutableEntry<>(UNKNOWN, ((LocalAddress) socketAddress).id());
76         } else {
77             return new SimpleImmutableEntry<>(UNKNOWN, UNKNOWN);
78         }
79     }
80 }