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.EpollServerSocketChannel;
22 import io.netty.channel.socket.SocketChannel;
23 import io.netty.channel.socket.nio.NioServerSocketChannel;
24 import io.netty.util.concurrent.DefaultPromise;
25 import io.netty.util.concurrent.EventExecutor;
26 import io.netty.util.concurrent.GlobalEventExecutor;
27 import io.netty.util.concurrent.Promise;
28 import java.io.Closeable;
29 import java.net.InetSocketAddress;
30 import org.opendaylight.protocol.concepts.KeyMapping;
31 import org.opendaylight.protocol.pcep.PCEPDispatcher;
32 import org.opendaylight.protocol.pcep.PCEPPeerProposal;
33 import org.opendaylight.protocol.pcep.PCEPSessionListenerFactory;
34 import org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactory;
35 import org.opendaylight.protocol.pcep.spi.MessageRegistry;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Implementation of PCEPDispatcher.
42 public class PCEPDispatcherImpl implements PCEPDispatcher, Closeable {
43 private static final Logger LOG = LoggerFactory.getLogger(PCEPDispatcherImpl.class);
44 private static final Integer SOCKET_BACKLOG_SIZE = 128;
45 private final PCEPSessionNegotiatorFactory snf;
46 private final PCEPHandlerFactory hf;
49 private final EventLoopGroup bossGroup;
50 private final EventLoopGroup workerGroup;
51 private final EventExecutor executor;
52 private Optional<KeyMapping> keys;
55 * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
57 * @param registry a message registry
58 * @param negotiatorFactory a negotiation factory
59 * @param bossGroup accepts an incoming connection
60 * @param workerGroup handles the traffic of accepted connection
62 public PCEPDispatcherImpl(final MessageRegistry registry,
63 final PCEPSessionNegotiatorFactory negotiatorFactory,
64 final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
65 this.snf = Preconditions.checkNotNull(negotiatorFactory);
66 this.hf = new PCEPHandlerFactory(registry);
67 if (Epoll.isAvailable()) {
68 this.bossGroup = new EpollEventLoopGroup();
69 this.workerGroup = new EpollEventLoopGroup();
71 this.bossGroup = Preconditions.checkNotNull(bossGroup);
72 this.workerGroup = Preconditions.checkNotNull(workerGroup);
74 this.executor = Preconditions.checkNotNull(GlobalEventExecutor.INSTANCE);
78 public synchronized ChannelFuture createServer(final InetSocketAddress address,
79 final PCEPSessionListenerFactory listenerFactory, final PCEPPeerProposal peerProposal) {
80 return createServer(address, Optional.<KeyMapping>absent(), listenerFactory, peerProposal);
84 public synchronized ChannelFuture createServer(final InetSocketAddress address, final Optional<KeyMapping> keys,
85 final PCEPSessionListenerFactory listenerFactory, final PCEPPeerProposal peerProposal) {
88 final ChannelPipelineInitializer initializer = (ch, promise) -> {
89 ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getDecoders());
90 ch.pipeline().addLast("negotiator", PCEPDispatcherImpl.this.snf.getSessionNegotiator(listenerFactory, ch, promise, peerProposal));
91 ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getEncoders());
94 final ServerBootstrap b = createServerBootstrap(initializer);
95 final ChannelFuture f = b.bind(address);
96 LOG.debug("Initiated server {} at {}.", f, address);
98 this.keys = Optional.absent();
102 protected ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer initializer) {
103 final ServerBootstrap b = new ServerBootstrap();
104 b.childHandler(new ChannelInitializer<SocketChannel>() {
106 protected void initChannel(final SocketChannel ch) {
107 initializer.initializeChannel(ch, new DefaultPromise(PCEPDispatcherImpl.this.executor));
110 b.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);
112 b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
114 if (Epoll.isAvailable()) {
115 b.channel(EpollServerSocketChannel.class);
117 b.channel(NioServerSocketChannel.class);
119 if (this.keys.isPresent()) {
120 if (Epoll.isAvailable()) {
121 b.option(EpollChannelOption.TCP_MD5SIG, this.keys.get());
123 throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
127 // Make sure we are doing round-robin processing
128 b.childOption(ChannelOption.MAX_MESSAGES_PER_READ, 1);
130 if (b.group() == null) {
131 b.group(this.bossGroup, this.workerGroup);
138 public void close() {
139 if (Epoll.isAvailable()) {
140 this.workerGroup.shutdownGracefully().awaitUninterruptibly();
141 this.bossGroup.shutdownGracefully().awaitUninterruptibly();
145 protected interface ChannelPipelineInitializer {
146 void initializeChannel(SocketChannel socketChannel, Promise<PCEPSessionImpl> promise);
150 public PCEPSessionNegotiatorFactory getPCEPSessionNegotiatorFactory() {