Merge "Migrate NetconfHelloMessage's use of Optional"
[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 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;
18 import java.util.Map;
19 import java.util.Optional;
20 import org.opendaylight.netconf.api.NetconfDocumentedException;
21 import org.opendaylight.netconf.api.NetconfServerSessionPreferences;
22 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
23 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
24 import org.opendaylight.netconf.nettyutil.AbstractNetconfSessionNegotiator;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class NetconfServerSessionNegotiator
29         extends AbstractNetconfSessionNegotiator<NetconfServerSessionPreferences, NetconfServerSession,
30                 NetconfServerSessionListener> {
31
32     private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSessionNegotiator.class);
33
34     private static final String UNKNOWN = "unknown";
35
36     protected NetconfServerSessionNegotiator(
37             NetconfServerSessionPreferences sessionPreferences,
38             Promise<NetconfServerSession> promise, Channel channel,
39             Timer timer, NetconfServerSessionListener sessionListener,
40             long connectionTimeoutMillis) {
41         super(sessionPreferences, promise, channel, timer, sessionListener,
42                 connectionTimeoutMillis);
43     }
44
45     @Override
46     protected void handleMessage(NetconfHelloMessage netconfMessage)
47             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     @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
56     protected NetconfServerSession getSession(
57             NetconfServerSessionListener sessionListener, Channel channel,
58             NetconfHelloMessage message) {
59         Optional<NetconfHelloMessageAdditionalHeader> additionalHeader = message
60                 .getAdditionalHeader();
61
62         NetconfHelloMessageAdditionalHeader parsedHeader;
63         if (additionalHeader.isPresent()) {
64             parsedHeader = additionalHeader.get();
65         } else {
66
67             parsedHeader = new NetconfHelloMessageAdditionalHeader(UNKNOWN,
68                     getHostName(channel.localAddress()).getValue(),
69                     getHostName(channel.localAddress()).getKey(), "tcp",
70                     "client");
71
72         }
73
74         LOG.debug("Additional header from hello parsed as {} from {}",
75                 parsedHeader, additionalHeader);
76
77         return new NetconfServerSession(sessionListener, channel,
78                 getSessionPreferences().getSessionId(), parsedHeader);
79     }
80
81     /**
82      * Get a name of the host.
83      *
84      * @param socketAddress type of socket address LocalAddress, or
85      *                      InetSocketAddress, for others returns unknown
86      * @return Two values - port and host of socket address
87      */
88     protected static Map.Entry<String, String> getHostName(
89             SocketAddress socketAddress) {
90
91         if (socketAddress instanceof InetSocketAddress) {
92
93             InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
94
95             return new AbstractMap.SimpleImmutableEntry<>(
96                     Integer.toString(inetSocketAddress.getPort()),
97                     inetSocketAddress.getHostString());
98
99         } else if (socketAddress instanceof LocalAddress) {
100
101             return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN,
102                     ((LocalAddress) socketAddress).id());
103
104         }
105         return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN, UNKNOWN);
106
107     }
108
109 }