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