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 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;
26 import java.io.Closeable;
27 import java.net.InetSocketAddress;
29 public class NetconfClientDispatcher extends AbstractDispatcher<NetconfClientSession, NetconfClientSessionListener> implements Closeable {
31 private static final Logger logger = LoggerFactory.getLogger(NetconfClientDispatcher.class);
33 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
34 private final HashedWheelTimer timer;
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);
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);
52 public Future<NetconfClientSession> createClient(InetSocketAddress address,
53 final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
55 return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
58 public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
59 initialize(ch, promise);
62 private void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
63 new ClientChannelInitializer(negotiatorFactory, sessionListener).initialize(ch, promise);
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);
73 return super.createReconnectingClient(address, connectStrategyFactory, reestablishStrategy,
74 new PipelineInitializer<NetconfClientSession>() {
76 public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
77 init.initialize(ch, promise);
82 private static class ClientChannelInitializer extends AbstractChannelInitializer<NetconfClientSession> {
84 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
85 private final NetconfClientSessionListener sessionListener;
87 private ClientChannelInitializer(NetconfClientSessionNegotiatorFactory negotiatorFactory,
88 NetconfClientSessionListener sessionListener) {
89 this.negotiatorFactory = negotiatorFactory;
90 this.sessionListener = sessionListener;
94 public void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
95 super.initialize(ch,promise);
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>() {
104 public NetconfClientSessionListener getSessionListener() {
105 return sessionListener;
112 public void close() {
115 } catch (Exception e) {
116 logger.debug("Ignoring exception while closing {}", timer, e);