Merge "Refactor Additional header for netconf hello message."
[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 io.netty.channel.ChannelFuture;
12 import io.netty.channel.EventLoopGroup;
13 import io.netty.channel.socket.SocketChannel;
14 import io.netty.util.concurrent.Promise;
15
16 import java.net.InetSocketAddress;
17
18 import org.opendaylight.controller.netconf.impl.util.DeserializerExceptionHandler;
19 import org.opendaylight.controller.netconf.util.AbstractChannelInitializer;
20 import org.opendaylight.protocol.framework.AbstractDispatcher;
21
22 public class NetconfServerDispatcher extends AbstractDispatcher<NetconfServerSession, NetconfServerSessionListener> {
23
24     private final ServerChannelInitializer initializer;
25
26     public NetconfServerDispatcher(ServerChannelInitializer serverChannelInitializer, EventLoopGroup bossGroup,
27             EventLoopGroup workerGroup) {
28         super(bossGroup, workerGroup);
29         this.initializer = serverChannelInitializer;
30     }
31
32     public ChannelFuture createServer(InetSocketAddress address) {
33
34         return super.createServer(address, new PipelineInitializer<NetconfServerSession>() {
35             @Override
36             public void initializeChannel(final SocketChannel ch, final Promise<NetconfServerSession> promise) {
37                 initializer.initialize(ch, promise);
38             }
39         });
40     }
41
42     public static class ServerChannelInitializer extends AbstractChannelInitializer<NetconfServerSession> {
43
44         public static final String DESERIALIZER_EX_HANDLER_KEY = "deserializerExHandler";
45
46         private final NetconfServerSessionNegotiatorFactory negotiatorFactory;
47         private final NetconfServerSessionListenerFactory listenerFactory;
48
49         public ServerChannelInitializer(NetconfServerSessionNegotiatorFactory negotiatorFactory,
50                                             NetconfServerSessionListenerFactory listenerFactory) {
51             this.negotiatorFactory = negotiatorFactory;
52             this.listenerFactory = listenerFactory;
53         }
54
55         @Override
56         protected void initializeMessageDecoder(SocketChannel ch) {
57             super.initializeMessageDecoder(ch);
58             ch.pipeline().addLast(DESERIALIZER_EX_HANDLER_KEY, new DeserializerExceptionHandler());
59         }
60
61         @Override
62         protected void initializeSessionNegotiator(SocketChannel ch, Promise<NetconfServerSession> promise) {
63             ch.pipeline().addAfter(DESERIALIZER_EX_HANDLER_KEY, AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR, negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
64         }
65     }
66
67 }