882d368a1af3648b06579903769d48026ecdf38c
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / NetconfServerDispatcher.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.impl;
10
11 import com.google.common.base.Optional;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.channel.socket.SocketChannel;
15 import io.netty.util.concurrent.Promise;
16 import org.opendaylight.controller.netconf.api.NetconfSession;
17 import org.opendaylight.controller.netconf.impl.util.DeserializerExceptionHandler;
18 import org.opendaylight.controller.netconf.util.AbstractSslChannelInitializer;
19 import org.opendaylight.protocol.framework.AbstractDispatcher;
20
21 import javax.net.ssl.SSLContext;
22 import javax.net.ssl.SSLEngine;
23 import java.net.InetSocketAddress;
24
25 public class NetconfServerDispatcher extends AbstractDispatcher<NetconfSession, NetconfServerSessionListener> {
26
27     private final ServerSslChannelInitializer initializer;
28
29     public NetconfServerDispatcher(ServerSslChannelInitializer serverChannelInitializer, EventLoopGroup bossGroup,
30             EventLoopGroup workerGroup) {
31         super(bossGroup, workerGroup);
32         this.initializer = serverChannelInitializer;
33     }
34
35     // TODO test create server with same address twice
36     public ChannelFuture createServer(InetSocketAddress address) {
37
38         return super.createServer(address, new PipelineInitializer<NetconfSession>() {
39             @Override
40             public void initializeChannel(final SocketChannel ch, final Promise<NetconfSession> promise) {
41                 initializer.initialize(ch, promise);
42             }
43         });
44     }
45
46     public static class ServerSslChannelInitializer extends AbstractSslChannelInitializer {
47
48         private final NetconfServerSessionNegotiatorFactory negotiatorFactory;
49         private final NetconfServerSessionListenerFactory listenerFactory;
50
51         public ServerSslChannelInitializer(Optional<SSLContext> maybeContext,
52                                             NetconfServerSessionNegotiatorFactory negotiatorFactory,
53                                             NetconfServerSessionListenerFactory listenerFactory) {
54             super(maybeContext);
55             this.negotiatorFactory = negotiatorFactory;
56             this.listenerFactory = listenerFactory;
57         }
58
59         @Override
60         protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
61             ch.pipeline().addLast("deserializerExHandler", new DeserializerExceptionHandler());
62             ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
63         }
64
65         @Override
66         protected void initSslEngine(SSLEngine sslEngine) {
67             sslEngine.setUseClientMode(false);
68         }
69     }
70
71 }