Bug 8153: Enforce check-style on netconf-impl
[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
9 package org.opendaylight.netconf.impl;
10
11 import com.google.common.base.Optional;
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;
19 import java.util.Map;
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     protected NetconfServerSession getSession(
56             NetconfServerSessionListener sessionListener, Channel channel,
57             NetconfHelloMessage message) {
58         Optional<NetconfHelloMessageAdditionalHeader> additionalHeader = message
59                 .getAdditionalHeader();
60
61         NetconfHelloMessageAdditionalHeader parsedHeader;
62         if (additionalHeader.isPresent()) {
63             parsedHeader = additionalHeader.get();
64         } else {
65
66             parsedHeader = new NetconfHelloMessageAdditionalHeader(UNKNOWN,
67                     getHostName(channel.localAddress()).getValue(),
68                     getHostName(channel.localAddress()).getKey(), "tcp",
69                     "client");
70
71         }
72
73         LOG.debug("Additional header from hello parsed as {} from {}",
74                 parsedHeader, additionalHeader);
75
76         return new NetconfServerSession(sessionListener, channel,
77                 getSessionPreferences().getSessionId(), parsedHeader);
78     }
79
80     /**
81      * Get a name of the host.
82      *
83      * @param socketAddress type of socket address LocalAddress, or
84      *                      InetSocketAddress, for others returns unknown
85      * @return Two values - port and host of socket address
86      */
87     protected static Map.Entry<String, String> getHostName(
88             SocketAddress socketAddress) {
89
90         if (socketAddress instanceof InetSocketAddress) {
91
92             InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
93
94             return new AbstractMap.SimpleImmutableEntry<>(
95                     Integer.toString(inetSocketAddress.getPort()),
96                     inetSocketAddress.getHostString());
97
98         } else if (socketAddress instanceof LocalAddress) {
99
100             return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN,
101                     ((LocalAddress) socketAddress).id());
102
103         }
104         return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN, UNKNOWN);
105
106     }
107
108 }