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