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