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