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 org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.api.NetconfServerSessionPreferences;
18 import org.opendaylight.controller.netconf.nettyutil.AbstractNetconfSessionNegotiator;
19 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
20 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import java.net.InetSocketAddress;
25 import java.net.SocketAddress;
26 import java.util.AbstractMap;
29 public class NetconfServerSessionNegotiator
31 AbstractNetconfSessionNegotiator<NetconfServerSessionPreferences, NetconfServerSession, NetconfServerSessionListener> {
33 static final Logger logger = LoggerFactory
34 .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 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 logger.debug("Additional header from hello parsed as {} from {}",
75 parsedHeader, additionalHeader);
77 return new NetconfServerSession(sessionListener, channel,
78 getSessionPreferences().getSessionId(), parsedHeader);
82 * @param socketAddress
83 * type of socket address LocalAddress, or
84 * InetSocketAddress, for others returns unknown
85 * @return Map<port, host > two values - port and host of socket address
87 protected static Map.Entry<String, String> getHostName(
88 SocketAddress socketAddress) {
90 if (socketAddress instanceof InetSocketAddress) {
92 InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
94 return new AbstractMap.SimpleImmutableEntry<>(
95 Integer.toString(inetSocketAddress.getPort()),
96 inetSocketAddress.getHostString());
98 } else if (socketAddress instanceof LocalAddress) {
100 return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN,
101 ((LocalAddress) socketAddress).id());
104 return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN, UNKNOWN);