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 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;
43 * Implementation of PCEPDispatcher.
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;
54 private Optional<KeyMapping> keys;
57 * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
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
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();
73 this.bossGroup = Preconditions.checkNotNull(bossGroup);
74 this.workerGroup = Preconditions.checkNotNull(workerGroup);
76 this.executor = Preconditions.checkNotNull(GlobalEventExecutor.INSTANCE);
80 public final synchronized ChannelFuture createServer(final InetSocketAddress address,
81 final PCEPSessionListenerFactory listenerFactory, final PCEPPeerProposal peerProposal) {
82 return createServer(address, Optional.absent(), listenerFactory, peerProposal);
86 public final synchronized ChannelFuture createServer(final InetSocketAddress address, final Optional<KeyMapping> keys,
87 final PCEPSessionListenerFactory listenerFactory, final PCEPPeerProposal peerProposal) {
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());
96 final ServerBootstrap b = createServerBootstrap(initializer);
97 final ChannelFuture f = b.bind(address);
98 LOG.debug("Initiated server {} at {}.", f, address);
100 this.keys = Optional.absent();
104 synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer initializer) {
105 final ServerBootstrap b = new ServerBootstrap();
106 b.childHandler(new ChannelInitializer<SocketChannel>() {
108 protected void initChannel(final SocketChannel ch) {
109 initializer.initializeChannel(ch, new DefaultPromise(PCEPDispatcherImpl.this.executor));
112 b.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);
114 b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
116 if (Epoll.isAvailable()) {
117 b.channel(EpollServerSocketChannel.class);
118 b.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
120 b.channel(NioServerSocketChannel.class);
122 if (this.keys.isPresent()) {
123 if (Epoll.isAvailable()) {
124 b.option(EpollChannelOption.TCP_MD5SIG, this.keys.get());
126 throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
130 // Make sure we are doing round-robin processing
131 b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, 1);
133 if (b.group() == null) {
134 b.group(this.bossGroup, this.workerGroup);
141 public final void close() {
142 if (Epoll.isAvailable()) {
143 this.workerGroup.shutdownGracefully().awaitUninterruptibly();
144 this.bossGroup.shutdownGracefully().awaitUninterruptibly();
148 protected interface ChannelPipelineInitializer {
149 void initializeChannel(SocketChannel socketChannel, Promise<PCEPSessionImpl> promise);
153 public final PCEPSessionNegotiatorFactory getPCEPSessionNegotiatorFactory() {