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 com.google.common.base.Preconditions;
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.AbstractSslChannelInitializer;
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;
26 import javax.net.ssl.SSLContext;
27 import javax.net.ssl.SSLEngine;
28 import java.net.InetSocketAddress;
30 public class NetconfClientDispatcher extends AbstractDispatcher<NetconfClientSession, NetconfClientSessionListener> {
32 private final Optional<SSLContext> maybeContext;
33 private final NetconfClientSessionNegotiatorFactory negotatorFactory;
35 public NetconfClientDispatcher(final Optional<SSLContext> maybeContext) {
36 this.maybeContext = Preconditions.checkNotNull(maybeContext);
37 this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(new HashedWheelTimer());
40 public Future<NetconfClientSession> createClient(InetSocketAddress address,
41 final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
43 return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
46 public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
47 initialize(ch, promise);
50 private void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
51 new ClientSslChannelInitializer(maybeContext, negotatorFactory, sessionListener).initialize(ch, promise);
56 private static class ClientSslChannelInitializer extends AbstractSslChannelInitializer {
58 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
59 private final NetconfClientSessionListener sessionListener;
61 private ClientSslChannelInitializer(Optional<SSLContext> maybeContext,
62 NetconfClientSessionNegotiatorFactory negotiatorFactory, NetconfClientSessionListener sessionListener) {
64 this.negotiatorFactory = negotiatorFactory;
65 this.sessionListener = sessionListener;
69 protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
70 ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
72 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
73 return sessionListener;
79 protected void initSslEngine(SSLEngine sslEngine) {
80 sslEngine.setUseClientMode(true);