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