Specialize protocol-framework to netconf
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / netconf / impl / NetconfServerDispatcherImpl.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 package org.opendaylight.netconf.impl;
9
10 import io.netty.channel.Channel;
11 import io.netty.channel.ChannelFuture;
12 import io.netty.channel.EventLoopGroup;
13 import io.netty.channel.local.LocalAddress;
14 import io.netty.channel.local.LocalServerChannel;
15 import io.netty.util.concurrent.Promise;
16 import java.net.InetSocketAddress;
17 import org.opendaylight.netconf.api.NetconfServerDispatcher;
18 import org.opendaylight.netconf.impl.util.DeserializerExceptionHandler;
19 import org.opendaylight.netconf.nettyutil.AbstractChannelInitializer;
20 import org.opendaylight.netconf.nettyutil.AbstractNetconfDispatcher;
21
22 public class NetconfServerDispatcherImpl extends AbstractNetconfDispatcher<NetconfServerSession,
23         NetconfServerSessionListener> implements NetconfServerDispatcher {
24
25     private final ServerChannelInitializer initializer;
26
27     public NetconfServerDispatcherImpl(ServerChannelInitializer serverChannelInitializer, EventLoopGroup bossGroup,
28                                        EventLoopGroup workerGroup) {
29         super(bossGroup, workerGroup);
30         this.initializer = serverChannelInitializer;
31     }
32
33     @Override
34     public ChannelFuture createServer(InetSocketAddress address) {
35         return super.createServer(address, initializer::initialize);
36     }
37
38     @Override
39     public ChannelFuture createLocalServer(LocalAddress address) {
40         return super.createServer(address, LocalServerChannel.class, initializer::initialize);
41     }
42
43     public static class ServerChannelInitializer extends AbstractChannelInitializer<NetconfServerSession> {
44
45         public static final String DESERIALIZER_EX_HANDLER_KEY = "deserializerExHandler";
46
47         private final NetconfServerSessionNegotiatorFactory negotiatorFactory;
48
49
50         public ServerChannelInitializer(NetconfServerSessionNegotiatorFactory negotiatorFactory) {
51             this.negotiatorFactory = negotiatorFactory;
52
53         }
54
55         @Override
56         protected void initializeMessageDecoder(Channel ch) {
57             super.initializeMessageDecoder(ch);
58             ch.pipeline().addLast(DESERIALIZER_EX_HANDLER_KEY, new DeserializerExceptionHandler());
59         }
60
61         @Override
62         protected void initializeSessionNegotiator(Channel ch, Promise<NetconfServerSession> promise) {
63             ch.pipeline().addAfter(DESERIALIZER_EX_HANDLER_KEY, AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR,
64                     negotiatorFactory.getSessionNegotiator(null, ch, promise));
65         }
66     }
67 }