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;
16 import java.io.Closeable;
17 import java.net.InetSocketAddress;
18 import org.opendaylight.controller.netconf.api.NetconfMessage;
19 import org.opendaylight.controller.netconf.api.NetconfSession;
20 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
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.SessionListener;
25 import org.opendaylight.protocol.framework.SessionListenerFactory;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class NetconfClientDispatcher extends AbstractDispatcher<NetconfClientSession, NetconfClientSessionListener> implements Closeable {
31 private static final Logger logger = LoggerFactory.getLogger(NetconfClient.class);
33 private final NetconfClientSessionNegotiatorFactory negotatorFactory;
34 private final HashedWheelTimer timer;
36 public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup) {
37 super(bossGroup, workerGroup);
38 timer = new HashedWheelTimer();
39 this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer);
42 public Future<NetconfClientSession> createClient(InetSocketAddress address,
43 final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
45 return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
48 public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
49 initialize(ch, promise);
52 private void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
53 new ClientChannelInitializer( negotatorFactory, sessionListener).initialize(ch, promise);
58 private static class ClientChannelInitializer extends AbstractChannelInitializer {
60 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
61 private final NetconfClientSessionListener sessionListener;
63 private ClientChannelInitializer(NetconfClientSessionNegotiatorFactory negotiatorFactory,
64 NetconfClientSessionListener sessionListener) {
65 this.negotiatorFactory = negotiatorFactory;
66 this.sessionListener = sessionListener;
70 public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
71 super.initialize(ch,promise);
75 protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
76 ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
78 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
79 return sessionListener;
89 } catch (Exception e) {
90 logger.debug("Ignoring exception while closing {}", timer, e);