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