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