BUG 624 - Make netconf TCP port optional.
[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.annotations.VisibleForTesting;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelFuture;
14 import io.netty.channel.EventLoopGroup;
15 import io.netty.channel.local.LocalAddress;
16 import io.netty.channel.local.LocalChannel;
17 import io.netty.channel.local.LocalServerChannel;
18 import io.netty.channel.socket.SocketChannel;
19 import io.netty.util.concurrent.Promise;
20 import java.net.InetSocketAddress;
21 import org.opendaylight.controller.netconf.impl.util.DeserializerExceptionHandler;
22 import org.opendaylight.controller.netconf.nettyutil.AbstractChannelInitializer;
23 import org.opendaylight.protocol.framework.AbstractDispatcher;
24
25 public class NetconfServerDispatcher extends AbstractDispatcher<NetconfServerSession, NetconfServerSessionListener> {
26
27     private final ServerChannelInitializer initializer;
28
29     public NetconfServerDispatcher(ServerChannelInitializer serverChannelInitializer, EventLoopGroup bossGroup,
30             EventLoopGroup workerGroup) {
31         super(bossGroup, workerGroup);
32         this.initializer = serverChannelInitializer;
33     }
34
35     @VisibleForTesting
36     public ChannelFuture createServer(InetSocketAddress address) {
37
38         return super.createServer(address, new PipelineInitializer<NetconfServerSession>() {
39             @Override
40             public void initializeChannel(final SocketChannel ch, final Promise<NetconfServerSession> promise) {
41                 initializer.initialize(ch, promise);
42             }
43         });
44     }
45
46     public ChannelFuture createLocalServer(LocalAddress address) {
47         return super.createServer(address, LocalServerChannel.class, new ChannelPipelineInitializer<LocalChannel, NetconfServerSession>() {
48             @Override
49             public void initializeChannel(final LocalChannel ch, final Promise<NetconfServerSession> promise) {
50                 initializer.initialize(ch, promise);
51             }
52         });
53     }
54
55     public static class ServerChannelInitializer extends AbstractChannelInitializer<NetconfServerSession> {
56
57         public static final String DESERIALIZER_EX_HANDLER_KEY = "deserializerExHandler";
58
59         private final NetconfServerSessionNegotiatorFactory negotiatorFactory;
60
61
62         public ServerChannelInitializer(NetconfServerSessionNegotiatorFactory negotiatorFactory) {
63             this.negotiatorFactory = negotiatorFactory;
64
65         }
66
67         @Override
68         protected void initializeMessageDecoder(Channel ch) {
69             super.initializeMessageDecoder(ch);
70             ch.pipeline().addLast(DESERIALIZER_EX_HANDLER_KEY, new DeserializerExceptionHandler());
71         }
72
73         @Override
74         protected void initializeSessionNegotiator(Channel ch, Promise<NetconfServerSession> promise) {
75             ch.pipeline().addAfter(DESERIALIZER_EX_HANDLER_KEY, AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR,
76                     negotiatorFactory.getSessionNegotiator(null, ch, promise));
77         }
78     }
79 }