2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl;
10 import static java.util.Objects.requireNonNull;
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;
44 * Implementation of PCEPDispatcher.
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;
56 private KeyMapping keys;
59 * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
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
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();
75 this.bossGroup = requireNonNull(bossGroup);
76 this.workerGroup = requireNonNull(workerGroup);
78 this.executor = requireNonNull(GlobalEventExecutor.INSTANCE);
82 public final synchronized ChannelFuture createServer(final InetSocketAddress address,
83 final PCEPSessionListenerFactory listenerFactory, final PCEPPeerProposal peerProposal) {
84 return createServer(address, KeyMapping.getKeyMapping(), listenerFactory, peerProposal);
88 public final synchronized ChannelFuture createServer(final InetSocketAddress address, final KeyMapping keys,
89 final PCEPSessionListenerFactory listenerFactory, final PCEPPeerProposal peerProposal) {
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());
98 final ServerBootstrap b = createServerBootstrap(initializer);
99 final ChannelFuture f = b.bind(address);
100 LOG.debug("Initiated server {} at {}.", f, address);
102 this.keys = KeyMapping.getKeyMapping();
106 synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer initializer) {
107 final ServerBootstrap b = new ServerBootstrap();
108 b.childHandler(new ChannelInitializer<SocketChannel>() {
110 protected void initChannel(final SocketChannel ch) {
111 initializer.initializeChannel(ch, new DefaultPromise<>(PCEPDispatcherImpl.this.executor));
114 b.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);
116 b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
118 if (Epoll.isAvailable()) {
119 b.channel(EpollServerSocketChannel.class);
120 b.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
122 b.channel(NioServerSocketChannel.class);
124 if (!this.keys.isEmpty()) {
125 if (Epoll.isAvailable()) {
126 b.option(EpollChannelOption.TCP_MD5SIG, this.keys);
128 throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
132 // Make sure we are doing round-robin processing
133 b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, 1);
135 if (b.group() == null) {
136 b.group(this.bossGroup, this.workerGroup);
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);
150 protected interface ChannelPipelineInitializer {
151 void initializeChannel(SocketChannel socketChannel, Promise<PCEPSessionImpl> promise);
155 public final PCEPSessionNegotiatorFactory<?> getPCEPSessionNegotiatorFactory() {