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.api.NetconfSession;
21 import org.opendaylight.controller.netconf.util.AbstractChannelInitializer;
22 import org.opendaylight.protocol.framework.AbstractDispatcher;
23 import org.opendaylight.protocol.framework.ReconnectStrategy;
24 import org.opendaylight.protocol.framework.SessionListenerFactory;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.base.Optional;
30 public class NetconfClientDispatcher extends AbstractDispatcher<NetconfClientSession, NetconfClientSessionListener> implements Closeable {
32 private static final Logger logger = LoggerFactory.getLogger(NetconfClient.class);
34 private final NetconfClientSessionNegotiatorFactory negotatorFactory;
35 private final HashedWheelTimer timer;
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);
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);
49 public Future<NetconfClientSession> createClient(InetSocketAddress address,
50 final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
52 return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
55 public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
56 initialize(ch, promise);
59 private void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
60 new ClientChannelInitializer( negotatorFactory, sessionListener).initialize(ch, promise);
65 private static class ClientChannelInitializer extends AbstractChannelInitializer {
67 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
68 private final NetconfClientSessionListener sessionListener;
70 private ClientChannelInitializer(NetconfClientSessionNegotiatorFactory negotiatorFactory,
71 NetconfClientSessionListener sessionListener) {
72 this.negotiatorFactory = negotiatorFactory;
73 this.sessionListener = sessionListener;
77 public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
78 super.initialize(ch,promise);
82 protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
83 ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(
84 new SessionListenerFactory<NetconfClientSessionListener>() {
86 public NetconfClientSessionListener getSessionListener() {
87 return sessionListener;
97 } catch (Exception e) {
98 logger.debug("Ignoring exception while closing {}", timer, e);