Fix checkstyle warnings in netconf-impl.
[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 java.net.InetSocketAddress;
17 import java.net.SocketAddress;
18 import java.util.AbstractMap;
19 import java.util.Map;
20 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
21 import org.opendaylight.controller.netconf.api.NetconfServerSessionPreferences;
22 import org.opendaylight.controller.netconf.nettyutil.AbstractNetconfSessionNegotiator;
23 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
24 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class NetconfServerSessionNegotiator
29         extends
30         AbstractNetconfSessionNegotiator<NetconfServerSessionPreferences, NetconfServerSession, 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      * @param socketAddress
82      *            type of socket address LocalAddress, or
83      *            InetSocketAddress, for others returns unknown
84      * @return Map<port, host > two values - port and host of socket address
85      */
86     protected static Map.Entry<String, String> getHostName(
87             SocketAddress socketAddress) {
88
89         if (socketAddress instanceof InetSocketAddress) {
90
91             InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
92
93             return new AbstractMap.SimpleImmutableEntry<>(
94                     Integer.toString(inetSocketAddress.getPort()),
95                     inetSocketAddress.getHostString());
96
97         } else if (socketAddress instanceof LocalAddress) {
98
99             return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN,
100                     ((LocalAddress) socketAddress).id());
101
102         }
103         return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN, UNKNOWN);
104
105     }
106
107 }