e32d5c7e451be7f4492b95ad499c11e5dc988627
[transportpce.git] / tests / honeynode / 1.2.1 / 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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import io.netty.channel.Channel;
14 import io.netty.channel.local.LocalAddress;
15 import io.netty.util.Timer;
16 import io.netty.util.concurrent.Promise;
17 import java.net.InetSocketAddress;
18 import java.net.SocketAddress;
19 import java.util.AbstractMap;
20 import java.util.Map;
21 import org.opendaylight.netconf.api.NetconfDocumentedException;
22 import org.opendaylight.netconf.api.NetconfServerSessionPreferences;
23 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
24 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
25 import org.opendaylight.netconf.nettyutil.AbstractNetconfSessionNegotiator;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class NetconfServerSessionNegotiator
30         extends AbstractNetconfSessionNegotiator<NetconfServerSessionPreferences, NetconfServerSession,
31                 NetconfServerSessionListener> {
32
33     private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSessionNegotiator.class);
34
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     @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
57     protected NetconfServerSession getSession(
58             NetconfServerSessionListener sessionListener, Channel channel,
59             NetconfHelloMessage message) {
60         Optional<NetconfHelloMessageAdditionalHeader> additionalHeader = message
61                 .getAdditionalHeader();
62
63         NetconfHelloMessageAdditionalHeader parsedHeader;
64         if (additionalHeader.isPresent()) {
65             parsedHeader = additionalHeader.get();
66         } else {
67
68             parsedHeader = new NetconfHelloMessageAdditionalHeader(UNKNOWN,
69                     getHostName(channel.localAddress()).getValue(),
70                     getHostName(channel.localAddress()).getKey(), "tcp",
71                     "client");
72
73         }
74
75         LOG.debug("Additional header from hello parsed as {} from {}",
76                 parsedHeader, additionalHeader);
77
78         return new NetconfServerSession(sessionListener, channel,
79                 getSessionPreferences().getSessionId(), parsedHeader);
80     }
81
82     /**
83      * Get a name of the host.
84      *
85      * @param socketAddress type of socket address LocalAddress, or
86      *                      InetSocketAddress, for others returns unknown
87      * @return Two values - port and host of socket address
88      */
89     protected static Map.Entry<String, String> getHostName(
90             SocketAddress socketAddress) {
91
92         if (socketAddress instanceof InetSocketAddress) {
93
94             InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
95
96             return new AbstractMap.SimpleImmutableEntry<>(
97                     Integer.toString(inetSocketAddress.getPort()),
98                     inetSocketAddress.getHostString());
99
100         } else if (socketAddress instanceof LocalAddress) {
101
102             return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN,
103                     ((LocalAddress) socketAddress).id());
104
105         }
106         return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN, UNKNOWN);
107
108     }
109
110 }