6fc4da026f38acc3add7538f3c172e2c6ee01a0f
[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
27 import javax.net.ssl.SSLContext;
28 import javax.net.ssl.SSLEngine;
29 import java.net.InetSocketAddress;
30
31 public class NetconfClientDispatcher extends AbstractDispatcher<NetconfClientSession, NetconfClientSessionListener> {
32
33     private final Optional<SSLContext> maybeContext;
34     private final NetconfClientSessionNegotiatorFactory negotatorFactory;
35
36     public NetconfClientDispatcher(final Optional<SSLContext> maybeContext, EventLoopGroup bossGroup, EventLoopGroup workerGroup) {
37         super(bossGroup, workerGroup);
38         this.maybeContext = Preconditions.checkNotNull(maybeContext);
39         this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(new HashedWheelTimer());
40     }
41
42     public Future<NetconfClientSession> createClient(InetSocketAddress address,
43             final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
44
45         return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
46
47             @Override
48             public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
49                 initialize(ch, promise);
50             }
51
52             private void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
53                 new ClientSslChannelInitializer(maybeContext, negotatorFactory, sessionListener).initialize(ch, promise);
54             }
55         });
56     }
57
58     private static class ClientSslChannelInitializer extends AbstractSslChannelInitializer {
59
60         private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
61         private final NetconfClientSessionListener sessionListener;
62
63         private ClientSslChannelInitializer(Optional<SSLContext> maybeContext,
64                                             NetconfClientSessionNegotiatorFactory negotiatorFactory, NetconfClientSessionListener sessionListener) {
65             super(maybeContext);
66             this.negotiatorFactory = negotiatorFactory;
67             this.sessionListener = sessionListener;
68         }
69
70         @Override
71         protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
72             ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
73                 @Override
74                 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
75                     return sessionListener;
76                 }
77             }, ch, promise));
78         }
79
80         @Override
81         protected void initSslEngine(SSLEngine sslEngine) {
82             sslEngine.setUseClientMode(true);
83         }
84     }
85
86 }