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.controller.netconf.impl;
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;
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;
28 public class NetconfServerSessionNegotiator
30 AbstractNetconfSessionNegotiator<NetconfServerSessionPreferences, NetconfServerSession, 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 protected NetconfServerSession getSession(
56 NetconfServerSessionListener sessionListener, Channel channel,
57 NetconfHelloMessage message) {
58 Optional<NetconfHelloMessageAdditionalHeader> additionalHeader = message
59 .getAdditionalHeader();
61 NetconfHelloMessageAdditionalHeader parsedHeader;
62 if (additionalHeader.isPresent()) {
63 parsedHeader = additionalHeader.get();
66 parsedHeader = new NetconfHelloMessageAdditionalHeader(UNKNOWN,
67 getHostName(channel.localAddress()).getValue(),
68 getHostName(channel.localAddress()).getKey(), "tcp",
73 LOG.debug("Additional header from hello parsed as {} from {}",
74 parsedHeader, additionalHeader);
76 return new NetconfServerSession(sessionListener, channel,
77 getSessionPreferences().getSessionId(), parsedHeader);
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
86 protected static Map.Entry<String, String> getHostName(
87 SocketAddress socketAddress) {
89 if (socketAddress instanceof InetSocketAddress) {
91 InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
93 return new AbstractMap.SimpleImmutableEntry<>(
94 Integer.toString(inetSocketAddress.getPort()),
95 inetSocketAddress.getHostString());
97 } else if (socketAddress instanceof LocalAddress) {
99 return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN,
100 ((LocalAddress) socketAddress).id());
103 return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN, UNKNOWN);