- netconf SSH bridge bundle
[controller.git] / opendaylight / netconf / netconf-client / src / main / java / org / opendaylight / controller / netconf / client / NetconfClientDispatcher.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.client;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.channel.socket.SocketChannel;
15 import io.netty.util.HashedWheelTimer;
16 import io.netty.util.concurrent.Future;
17 import io.netty.util.concurrent.Promise;
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.AbstractSslChannelInitializer;
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;
28
29 import javax.net.ssl.SSLContext;
30 import javax.net.ssl.SSLEngine;
31 import java.io.Closeable;
32 import java.net.InetSocketAddress;
33
34 public class NetconfClientDispatcher extends AbstractDispatcher<NetconfClientSession, NetconfClientSessionListener> implements Closeable {
35
36     private static final Logger logger = LoggerFactory.getLogger(NetconfClient.class);
37
38     private final Optional<SSLContext> maybeContext;
39     private final NetconfClientSessionNegotiatorFactory negotatorFactory;
40     private final HashedWheelTimer timer;
41
42     public NetconfClientDispatcher(final Optional<SSLContext> maybeContext, EventLoopGroup bossGroup, EventLoopGroup workerGroup) {
43         super(bossGroup, workerGroup);
44         this.maybeContext = Preconditions.checkNotNull(maybeContext);
45         timer = new HashedWheelTimer();
46         this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer);
47     }
48
49     public Future<NetconfClientSession> createClient(InetSocketAddress address,
50             final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
51
52         return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
53
54             @Override
55             public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
56                 initialize(ch, promise);
57             }
58
59             private void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
60                 new ClientSslChannelInitializer(maybeContext, negotatorFactory, sessionListener).initialize(ch, promise);
61             }
62         });
63     }
64
65     private static class ClientSslChannelInitializer extends AbstractSslChannelInitializer {
66
67         private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
68         private final NetconfClientSessionListener sessionListener;
69
70         private ClientSslChannelInitializer(Optional<SSLContext> maybeContext,
71                                             NetconfClientSessionNegotiatorFactory negotiatorFactory, NetconfClientSessionListener sessionListener) {
72             super(maybeContext);
73             this.negotiatorFactory = negotiatorFactory;
74             this.sessionListener = sessionListener;
75         }
76
77         @Override
78         protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
79             ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
80                 @Override
81                 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
82                     return sessionListener;
83                 }
84             }, ch, promise));
85         }
86
87         @Override
88         protected void initSslEngine(SSLEngine sslEngine) {
89             sslEngine.setUseClientMode(true);
90         }
91     }
92
93     @Override
94     public void close() {
95         try {
96             timer.stop();
97         } catch (Exception e) {
98             logger.debug("Ignoring exception while closing {}", timer, e);
99         }
100     }
101 }