2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.client;
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;
17 import java.io.Closeable;
18 import java.net.InetSocketAddress;
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;
29 import com.google.common.base.Optional;
31 public class NetconfClientDispatcher extends AbstractDispatcher<NetconfClientSession, NetconfClientSessionListener> implements Closeable {
33 private static final Logger logger = LoggerFactory.getLogger(NetconfClientDispatcher.class);
35 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
36 private final HashedWheelTimer timer;
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);
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);
54 public Future<NetconfClientSession> createClient(InetSocketAddress address,
55 final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
57 return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
60 public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
61 initialize(ch, promise);
64 private void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
65 new ClientChannelInitializer(negotiatorFactory, sessionListener).initialize(ch, promise);
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);
75 return super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategy,
76 new PipelineInitializer<NetconfClientSession>() {
78 public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
79 init.initialize(ch, promise);
84 private static class ClientChannelInitializer extends AbstractChannelInitializer<NetconfClientSession> {
86 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
87 private final NetconfClientSessionListener sessionListener;
89 private ClientChannelInitializer(NetconfClientSessionNegotiatorFactory negotiatorFactory,
90 NetconfClientSessionListener sessionListener) {
91 this.negotiatorFactory = negotiatorFactory;
92 this.sessionListener = sessionListener;
96 public void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
97 super.initialize(ch,promise);
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>() {
106 public NetconfClientSessionListener getSessionListener() {
107 return sessionListener;
114 public void close() {
117 } catch (Exception e) {
118 logger.debug("Ignoring exception while closing {}", timer, e);