Merge "CHange log level from warn to debug in ProtocolSessionPromise when connection...
[controller.git] / opendaylight / netconf / netconf-client / src / main / java / org / opendaylight / controller / netconf / client / NetconfClientDispatcher.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.client;
10
11 import io.netty.channel.EventLoopGroup;
12 import io.netty.channel.socket.SocketChannel;
13 import io.netty.util.HashedWheelTimer;
14 import io.netty.util.concurrent.Future;
15 import io.netty.util.concurrent.Promise;
16
17 import java.io.Closeable;
18 import java.net.InetSocketAddress;
19
20 import org.opendaylight.controller.netconf.util.AbstractChannelInitializer;
21 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
22 import org.opendaylight.protocol.framework.AbstractDispatcher;
23 import org.opendaylight.protocol.framework.ReconnectStrategy;
24 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
25 import org.opendaylight.protocol.framework.SessionListenerFactory;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import com.google.common.base.Optional;
30
31 public class NetconfClientDispatcher extends AbstractDispatcher<NetconfClientSession, NetconfClientSessionListener> implements Closeable {
32
33     private static final Logger logger = LoggerFactory.getLogger(NetconfClientDispatcher.class);
34
35     private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
36     private final HashedWheelTimer timer;
37
38     public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup,
39             long clientConnectionTimeoutMillis) {
40         super(bossGroup, workerGroup);
41         timer = new HashedWheelTimer();
42         this.negotiatorFactory = new NetconfClientSessionNegotiatorFactory(timer,
43                 Optional.<NetconfHelloMessageAdditionalHeader> absent(), clientConnectionTimeoutMillis);
44     }
45
46     public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup,
47             NetconfHelloMessageAdditionalHeader additionalHeader, long connectionTimeoutMillis) {
48         super(bossGroup, workerGroup);
49         timer = new HashedWheelTimer();
50         this.negotiatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.of(additionalHeader),
51                 connectionTimeoutMillis);
52     }
53
54     public Future<NetconfClientSession> createClient(InetSocketAddress address,
55             final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
56
57         return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
58
59             @Override
60             public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
61                 initialize(ch, promise);
62             }
63
64             private void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
65                 new ClientChannelInitializer(negotiatorFactory, sessionListener).initialize(ch, promise);
66             }
67         });
68     }
69
70     public Future<Void> createReconnectingClient(final InetSocketAddress address,
71             final NetconfClientSessionListener listener,
72             final ReconnectStrategyFactory connectStrategyFactory, final ReconnectStrategy reestablishStrategy) {
73         final ClientChannelInitializer init = new ClientChannelInitializer(negotiatorFactory, listener);
74
75         return super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategy,
76                 new PipelineInitializer<NetconfClientSession>() {
77             @Override
78             public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
79                 init.initialize(ch, promise);
80             }
81         });
82     }
83
84     private static class ClientChannelInitializer extends AbstractChannelInitializer<NetconfClientSession> {
85
86         private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
87         private final NetconfClientSessionListener sessionListener;
88
89         private ClientChannelInitializer(NetconfClientSessionNegotiatorFactory negotiatorFactory,
90                 NetconfClientSessionListener sessionListener) {
91             this.negotiatorFactory = negotiatorFactory;
92             this.sessionListener = sessionListener;
93         }
94
95         @Override
96         public void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
97                 super.initialize(ch,promise);
98         }
99
100         @Override
101         protected void initializeSessionNegotiator(SocketChannel ch, Promise<NetconfClientSession> promise) {
102             ch.pipeline().addAfter(NETCONF_MESSAGE_DECODER,  AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR,
103                     negotiatorFactory.getSessionNegotiator(
104                             new SessionListenerFactory<NetconfClientSessionListener>() {
105                                 @Override
106                                 public NetconfClientSessionListener getSessionListener() {
107                                     return sessionListener;
108                                 }
109                             }, ch, promise));
110         }
111     }
112
113     @Override
114     public void close() {
115         try {
116             timer.stop();
117         } catch (Exception e) {
118             logger.debug("Ignoring exception while closing {}", timer, e);
119         }
120     }
121 }