BUG-7673: Improve synchonization under BGP/PCEP Session
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPDispatcherImpl.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.protocol.pcep.impl;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import io.netty.bootstrap.ServerBootstrap;
13 import io.netty.buffer.PooledByteBufAllocator;
14 import io.netty.channel.ChannelFuture;
15 import io.netty.channel.ChannelInitializer;
16 import io.netty.channel.ChannelOption;
17 import io.netty.channel.EventLoopGroup;
18 import io.netty.channel.epoll.Epoll;
19 import io.netty.channel.epoll.EpollChannelOption;
20 import io.netty.channel.epoll.EpollEventLoopGroup;
21 import io.netty.channel.epoll.EpollMode;
22 import io.netty.channel.epoll.EpollServerSocketChannel;
23 import io.netty.channel.socket.SocketChannel;
24 import io.netty.channel.socket.nio.NioServerSocketChannel;
25 import io.netty.util.concurrent.DefaultPromise;
26 import io.netty.util.concurrent.EventExecutor;
27 import io.netty.util.concurrent.GlobalEventExecutor;
28 import io.netty.util.concurrent.Promise;
29 import java.io.Closeable;
30 import java.net.InetSocketAddress;
31 import javax.annotation.Nonnull;
32 import javax.annotation.concurrent.GuardedBy;
33 import org.opendaylight.protocol.concepts.KeyMapping;
34 import org.opendaylight.protocol.pcep.PCEPDispatcher;
35 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
36 import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
37 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
38 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Implementation of PCEPDispatcher.
44  */
45 public class PCEPDispatcherImpl implements PCEPDispatcher, Closeable {
46     private static final Logger LOG = LoggerFactory.getLogger(PCEPDispatcherImpl.class);
47     private static final Integer SOCKET_BACKLOG_SIZE = 128;
48     private final PCEPSessionNegotiatorFactory snf;
49     private final PCEPHandlerFactory hf;
50     private final EventLoopGroup bossGroup;
51     private final EventLoopGroup workerGroup;
52     private final EventExecutor executor;
53     @GuardedBy("this")
54     private Optional<KeyMapping> keys;
55
56     /**
57      * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
58      *
59      * @param registry a message registry
60      * @param negotiatorFactory a negotiation factory
61      * @param bossGroup accepts an incoming connection
62      * @param workerGroup handles the traffic of accepted connection
63      */
64     public PCEPDispatcherImpl(@Nonnull final MessageRegistry registry,
65         @Nonnull final PCEPSessionNegotiatorFactory negotiatorFactory,
66         @Nonnull final EventLoopGroup bossGroup, @Nonnull  final EventLoopGroup workerGroup) {
67         this.snf = Preconditions.checkNotNull(negotiatorFactory);
68         this.hf = new PCEPHandlerFactory(registry);
69         if (Epoll.isAvailable()) {
70             this.bossGroup = new EpollEventLoopGroup();
71             this.workerGroup = new EpollEventLoopGroup();
72         } else {
73             this.bossGroup = Preconditions.checkNotNull(bossGroup);
74             this.workerGroup = Preconditions.checkNotNull(workerGroup);
75         }
76         this.executor = Preconditions.checkNotNull(GlobalEventExecutor.INSTANCE);
77     }
78
79     @Override
80     public final synchronized ChannelFuture createServer(final InetSocketAddress address,
81         final PCEPSessionListenerFactory listenerFactory, final PCEPPeerProposal peerProposal) {
82         return createServer(address, Optional.absent(), listenerFactory, peerProposal);
83     }
84
85     @Override
86     public final synchronized ChannelFuture createServer(final InetSocketAddress address, final Optional<KeyMapping> keys,
87         final PCEPSessionListenerFactory listenerFactory, final PCEPPeerProposal peerProposal) {
88         this.keys = keys;
89
90         final ChannelPipelineInitializer initializer = (ch, promise) -> {
91             ch.pipeline().addLast(this.hf.getDecoders());
92             ch.pipeline().addLast("negotiator", this.snf.getSessionNegotiator(listenerFactory, ch, promise, peerProposal));
93             ch.pipeline().addLast(this.hf.getEncoders());
94         };
95
96         final ServerBootstrap b = createServerBootstrap(initializer);
97         final ChannelFuture f = b.bind(address);
98         LOG.debug("Initiated server {} at {}.", f, address);
99
100         this.keys = Optional.absent();
101         return f;
102     }
103
104     synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer initializer) {
105         final ServerBootstrap b = new ServerBootstrap();
106         b.childHandler(new ChannelInitializer<SocketChannel>() {
107             @Override
108             protected void initChannel(final SocketChannel ch) {
109                 initializer.initializeChannel(ch, new DefaultPromise(PCEPDispatcherImpl.this.executor));
110             }
111         });
112         b.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);
113
114         b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
115
116         if (Epoll.isAvailable()) {
117             b.channel(EpollServerSocketChannel.class);
118             b.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
119         } else {
120             b.channel(NioServerSocketChannel.class);
121         }
122         if (this.keys.isPresent()) {
123             if (Epoll.isAvailable()) {
124                 b.option(EpollChannelOption.TCP_MD5SIG, this.keys.get());
125             } else {
126                 throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
127             }
128         }
129
130         // Make sure we are doing round-robin processing
131         b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, 1);
132
133         if (b.group() == null) {
134             b.group(this.bossGroup, this.workerGroup);
135         }
136
137         return b;
138     }
139
140     @Override
141     public final void close() {
142         if (Epoll.isAvailable()) {
143             this.workerGroup.shutdownGracefully().awaitUninterruptibly();
144             this.bossGroup.shutdownGracefully().awaitUninterruptibly();
145         }
146     }
147
148     protected interface ChannelPipelineInitializer {
149         void initializeChannel(SocketChannel socketChannel, Promise<PCEPSessionImpl> promise);
150     }
151
152     @Override
153     public final PCEPSessionNegotiatorFactory getPCEPSessionNegotiatorFactory() {
154         return this.snf;
155     }
156 }