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
9 package org.opendaylight.netconf.impl;
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;
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;
29 public class NetconfServerSessionNegotiator
30 extends AbstractNetconfSessionNegotiator<NetconfServerSessionPreferences, NetconfServerSession,
31 NetconfServerSessionListener> {
33 private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSessionNegotiator.class);
35 private static final String UNKNOWN = "unknown";
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);
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);
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();
63 NetconfHelloMessageAdditionalHeader parsedHeader;
64 if (additionalHeader.isPresent()) {
65 parsedHeader = additionalHeader.get();
68 parsedHeader = new NetconfHelloMessageAdditionalHeader(UNKNOWN,
69 getHostName(channel.localAddress()).getValue(),
70 getHostName(channel.localAddress()).getKey(), "tcp",
75 LOG.debug("Additional header from hello parsed as {} from {}",
76 parsedHeader, additionalHeader);
78 return new NetconfServerSession(sessionListener, channel,
79 getSessionPreferences().getSessionId(), parsedHeader);
83 * Get a name of the host.
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
89 protected static Map.Entry<String, String> getHostName(
90 SocketAddress socketAddress) {
92 if (socketAddress instanceof InetSocketAddress) {
94 InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
96 return new AbstractMap.SimpleImmutableEntry<>(
97 Integer.toString(inetSocketAddress.getPort()),
98 inetSocketAddress.getHostString());
100 } else if (socketAddress instanceof LocalAddress) {
102 return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN,
103 ((LocalAddress) socketAddress).id());
106 return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN, UNKNOWN);