2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.impl;
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;
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;
28 public class NetconfServerSessionNegotiator
29 extends AbstractNetconfSessionNegotiator<NetconfServerSessionPreferences, NetconfServerSession,
30 NetconfServerSessionListener> {
32 private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSessionNegotiator.class);
34 private static final String UNKNOWN = "unknown";
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);
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);
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();
62 NetconfHelloMessageAdditionalHeader parsedHeader;
63 if (additionalHeader.isPresent()) {
64 parsedHeader = additionalHeader.get();
67 parsedHeader = new NetconfHelloMessageAdditionalHeader(UNKNOWN,
68 getHostName(channel.localAddress()).getValue(),
69 getHostName(channel.localAddress()).getKey(), "tcp",
74 LOG.debug("Additional header from hello parsed as {} from {}",
75 parsedHeader, additionalHeader);
77 return new NetconfServerSession(sessionListener, channel,
78 getSessionPreferences().getSessionId(), parsedHeader);
82 * Get a name of the host.
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
88 protected static Map.Entry<String, String> getHostName(
89 SocketAddress socketAddress) {
91 if (socketAddress instanceof InetSocketAddress) {
93 InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
95 return new AbstractMap.SimpleImmutableEntry<>(
96 Integer.toString(inetSocketAddress.getPort()),
97 inetSocketAddress.getHostString());
99 } else if (socketAddress instanceof LocalAddress) {
101 return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN,
102 ((LocalAddress) socketAddress).id());
105 return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN, UNKNOWN);