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.api.NetconfMessage;
18 import org.opendaylight.controller.netconf.api.NetconfSession;
19 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
20 import org.opendaylight.controller.netconf.util.AbstractChannelInitializer;
21 import org.opendaylight.protocol.framework.AbstractDispatcher;
22 import org.opendaylight.protocol.framework.ReconnectStrategy;
23 import org.opendaylight.protocol.framework.SessionListener;
24 import org.opendaylight.protocol.framework.SessionListenerFactory;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import java.io.Closeable;
29 import java.net.InetSocketAddress;
31 public class NetconfClientDispatcher extends AbstractDispatcher<NetconfClientSession, NetconfClientSessionListener> implements Closeable {
33 private static final Logger logger = LoggerFactory.getLogger(NetconfClient.class);
35 private final NetconfClientSessionNegotiatorFactory negotatorFactory;
36 private final HashedWheelTimer timer;
38 public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup, long clientConnectionTimeoutMillis) {
39 super(bossGroup, workerGroup);
40 timer = new HashedWheelTimer();
41 this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.<String>absent(), clientConnectionTimeoutMillis);
44 public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup, String additionalHeader, long connectionTimeoutMillis) {
45 super(bossGroup, workerGroup);
46 timer = new HashedWheelTimer();
47 this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.of(additionalHeader), connectionTimeoutMillis);
50 public Future<NetconfClientSession> createClient(InetSocketAddress address,
51 final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
53 return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
56 public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
57 initialize(ch, promise);
60 private void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
61 new ClientChannelInitializer( negotatorFactory, sessionListener).initialize(ch, promise);
66 private static class ClientChannelInitializer extends AbstractChannelInitializer {
68 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
69 private final NetconfClientSessionListener sessionListener;
71 private ClientChannelInitializer(NetconfClientSessionNegotiatorFactory negotiatorFactory,
72 NetconfClientSessionListener sessionListener) {
73 this.negotiatorFactory = negotiatorFactory;
74 this.sessionListener = sessionListener;
78 public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
79 super.initialize(ch,promise);
83 protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
84 ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
86 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
87 return sessionListener;
97 } catch (Exception e) {
98 logger.debug("Ignoring exception while closing {}", timer, e);