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.NetconfMessage;
21 import org.opendaylight.controller.netconf.api.NetconfSession;
22 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
23 import org.opendaylight.controller.netconf.util.AbstractChannelInitializer;
24 import org.opendaylight.protocol.framework.AbstractDispatcher;
25 import org.opendaylight.protocol.framework.ReconnectStrategy;
26 import org.opendaylight.protocol.framework.SessionListener;
27 import org.opendaylight.protocol.framework.SessionListenerFactory;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.google.common.base.Optional;
33 public class NetconfClientDispatcher extends AbstractDispatcher<NetconfClientSession, NetconfClientSessionListener> implements Closeable {
35 private static final Logger logger = LoggerFactory.getLogger(NetconfClient.class);
37 private final NetconfClientSessionNegotiatorFactory negotatorFactory;
38 private final HashedWheelTimer timer;
40 public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup) {
41 super(bossGroup, workerGroup);
42 timer = new HashedWheelTimer();
43 this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.<String>absent());
46 public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup, String additionalHeader) {
47 super(bossGroup, workerGroup);
48 timer = new HashedWheelTimer();
49 this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.of(additionalHeader));
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( negotatorFactory, sessionListener).initialize(ch, promise);
68 private static class ClientChannelInitializer extends AbstractChannelInitializer {
70 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
71 private final NetconfClientSessionListener sessionListener;
73 private ClientChannelInitializer(NetconfClientSessionNegotiatorFactory negotiatorFactory,
74 NetconfClientSessionListener sessionListener) {
75 this.negotiatorFactory = negotiatorFactory;
76 this.sessionListener = sessionListener;
80 public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
81 super.initialize(ch,promise);
85 protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
86 ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
88 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
89 return sessionListener;
99 } catch (Exception e) {
100 logger.debug("Ignoring exception while closing {}", timer, e);