BUG-2243 Fixing invalid hello message handling
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / 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.controller.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 org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.api.NetconfServerSessionPreferences;
18 import org.opendaylight.controller.netconf.nettyutil.AbstractNetconfSessionNegotiator;
19 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
20 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import java.net.InetSocketAddress;
25 import java.net.SocketAddress;
26 import java.util.AbstractMap;
27 import java.util.Map;
28
29 public class NetconfServerSessionNegotiator
30         extends
31         AbstractNetconfSessionNegotiator<NetconfServerSessionPreferences, NetconfServerSession, NetconfServerSessionListener> {
32
33     static final Logger logger = LoggerFactory
34             .getLogger(NetconfServerSessionNegotiator.class);
35     private static final String UNKNOWN = "unknown";
36
37     protected NetconfServerSessionNegotiator(
38             NetconfServerSessionPreferences sessionPreferences,
39             Promise<NetconfServerSession> promise, Channel channel,
40             Timer timer, NetconfServerSessionListener sessionListener,
41             long connectionTimeoutMillis) {
42         super(sessionPreferences, promise, channel, timer, sessionListener,
43                 connectionTimeoutMillis);
44     }
45
46     @Override
47     protected void handleMessage(NetconfHelloMessage netconfMessage)
48             throws NetconfDocumentedException {
49         NetconfServerSession session = getSessionForHelloMessage(netconfMessage);
50         replaceHelloMessageInboundHandler(session);
51         // Negotiation successful after all non hello messages were processed
52         negotiationSuccessful(session);
53     }
54
55     @Override
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         logger.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      * @param socketAddress
83      *            type of socket address LocalAddress, or
84      *            InetSocketAddress, for others returns unknown
85      * @return Map<port, host > 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 }