Merge "Increase timeout for waiting for broker service in sal-binding-it."
[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.api.NetconfMessage;
18 import org.opendaylight.controller.netconf.api.NetconfSession;
19 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
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.SessionListener;
24 import org.opendaylight.protocol.framework.SessionListenerFactory;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.io.Closeable;
29 import java.net.InetSocketAddress;
30
31 public class NetconfClientDispatcher extends AbstractDispatcher<NetconfClientSession, NetconfClientSessionListener> implements Closeable {
32
33     private static final Logger logger = LoggerFactory.getLogger(NetconfClient.class);
34
35     private final NetconfClientSessionNegotiatorFactory negotatorFactory;
36     private final HashedWheelTimer timer;
37
38     public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup, long clientConnectionTimeoutMillis) {
39         super(bossGroup, workerGroup);
40         timer = new HashedWheelTimer();
41         this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.<String>absent(), clientConnectionTimeoutMillis);
42     }
43
44     public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup, String additionalHeader, long connectionTimeoutMillis) {
45         super(bossGroup, workerGroup);
46         timer = new HashedWheelTimer();
47         this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.of(additionalHeader), connectionTimeoutMillis);
48     }
49
50     public Future<NetconfClientSession> createClient(InetSocketAddress address,
51             final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
52
53         return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
54
55             @Override
56             public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
57                 initialize(ch, promise);
58             }
59
60             private void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
61                 new ClientChannelInitializer( negotatorFactory, sessionListener).initialize(ch, promise);
62             }
63         });
64     }
65
66     private static class ClientChannelInitializer extends AbstractChannelInitializer {
67
68         private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
69         private final NetconfClientSessionListener sessionListener;
70
71         private ClientChannelInitializer(NetconfClientSessionNegotiatorFactory negotiatorFactory,
72                                             NetconfClientSessionListener sessionListener) {
73             this.negotiatorFactory = negotiatorFactory;
74             this.sessionListener = sessionListener;
75         }
76
77         @Override
78         public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
79                 super.initialize(ch,promise);
80         }
81
82         @Override
83         protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
84             ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
85                 @Override
86                 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
87                     return sessionListener;
88                 }
89             }, ch, promise));
90         }
91
92     }
93     @Override
94     public void close() {
95         try {
96             timer.stop();
97         } catch (Exception e) {
98             logger.debug("Ignoring exception while closing {}", timer, e);
99         }
100     }
101 }