Merge "Fixed typo in SnapshotBackedWriteTransaction class"
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / 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.controller.netconf.impl;
10
11 import java.net.InetSocketAddress;
12
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.api.NetconfServerSessionPreferences;
15 import org.opendaylight.controller.netconf.util.AbstractNetconfSessionNegotiator;
16 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
17 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import com.google.common.base.Optional;
22
23 import io.netty.channel.Channel;
24 import io.netty.util.Timer;
25 import io.netty.util.concurrent.Promise;
26
27 public class NetconfServerSessionNegotiator extends
28         AbstractNetconfSessionNegotiator<NetconfServerSessionPreferences, NetconfServerSession, NetconfServerSessionListener> {
29
30     static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionNegotiator.class);
31
32     protected NetconfServerSessionNegotiator(NetconfServerSessionPreferences sessionPreferences,
33             Promise<NetconfServerSession> promise, Channel channel, Timer timer, NetconfServerSessionListener sessionListener,
34             long connectionTimeoutMillis) {
35         super(sessionPreferences, promise, channel, timer, sessionListener, connectionTimeoutMillis);
36     }
37
38     @Override
39     protected void handleMessage(NetconfHelloMessage netconfMessage) throws NetconfDocumentedException {
40         NetconfServerSession session = getSessionForHelloMessage(netconfMessage);
41         replaceHelloMessageInboundHandler(session);
42         // Negotiation successful after all non hello messages were processed
43         negotiationSuccessful(session);
44     }
45
46     @Override
47     protected NetconfServerSession getSession(NetconfServerSessionListener sessionListener, Channel channel, NetconfHelloMessage message) {
48         Optional<NetconfHelloMessageAdditionalHeader> additionalHeader = message.getAdditionalHeader();
49
50         NetconfHelloMessageAdditionalHeader parsedHeader;
51         if (additionalHeader.isPresent()) {
52             parsedHeader = additionalHeader.get();
53         } else {
54             InetSocketAddress inetSocketAddress = (InetSocketAddress) channel.localAddress();
55             parsedHeader = new NetconfHelloMessageAdditionalHeader("unknown", inetSocketAddress.getHostString(), Integer.toString(inetSocketAddress.getPort()),
56                     "tcp", "client");
57         }
58
59         logger.debug("Additional header from hello parsed as {} from {}", parsedHeader, additionalHeader);
60
61         return new NetconfServerSession(sessionListener, channel, getSessionPreferences().getSessionId(), parsedHeader);
62     }
63
64 }