2 * Copyright (c) 2024 PANTHEON.tech, s.r.o. 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.bgp.rib.impl;
10 import static java.util.Objects.requireNonNull;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.bootstrap.ServerBootstrap;
14 import io.netty.channel.EventLoopGroup;
15 import io.netty.channel.epoll.Epoll;
16 import io.netty.channel.epoll.EpollChannelOption;
17 import io.netty.channel.epoll.EpollEventLoopGroup;
18 import io.netty.channel.epoll.EpollMode;
19 import io.netty.channel.epoll.EpollServerSocketChannel;
20 import io.netty.channel.epoll.EpollSocketChannel;
21 import io.netty.channel.nio.NioEventLoopGroup;
22 import io.netty.channel.socket.nio.NioServerSocketChannel;
23 import io.netty.channel.socket.nio.NioSocketChannel;
24 import java.util.concurrent.TimeUnit;
25 import javax.annotation.PreDestroy;
26 import javax.inject.Inject;
27 import javax.inject.Singleton;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.opendaylight.protocol.concepts.KeyMapping;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Deactivate;
35 @Component(service = BGPNettyGroups.class)
36 public final class BGPNettyGroups implements AutoCloseable {
37 private abstract static class AbstractImpl {
38 private final EventLoopGroup bossGroup;
39 private final EventLoopGroup workerGroup;
41 AbstractImpl(final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
42 this.bossGroup = requireNonNull(bossGroup);
43 this.workerGroup = requireNonNull(workerGroup);
46 abstract void setupBootstrap(Bootstrap bootstrap);
48 abstract void setupBootstrap(ServerBootstrap serverBootstrap);
50 abstract void setupKeys(Bootstrap bootstrap, KeyMapping keys);
53 private static final class EpollImpl extends AbstractImpl {
55 super(new EpollEventLoopGroup(), new EpollEventLoopGroup());
59 void setupBootstrap(final Bootstrap bootstrap) {
60 bootstrap.channel(EpollSocketChannel.class);
61 bootstrap.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
65 void setupBootstrap(final ServerBootstrap serverBootstrap) {
66 serverBootstrap.channel(EpollServerSocketChannel.class);
67 serverBootstrap.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
71 void setupKeys(final Bootstrap bootstrap, final KeyMapping keys) {
72 bootstrap.option(EpollChannelOption.TCP_MD5SIG, keys.asMap());
76 private static final class NioImpl extends AbstractImpl {
78 super(new NioEventLoopGroup(), new NioEventLoopGroup());
82 void setupBootstrap(final Bootstrap bootstrap) {
83 bootstrap.channel(NioSocketChannel.class);
87 void setupBootstrap(final ServerBootstrap serverBootstrap) {
88 serverBootstrap.channel(NioServerSocketChannel.class);
92 void setupKeys(final Bootstrap bootstrap, final KeyMapping keys) {
93 throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
97 private static final long TIMEOUT = 10;
99 private AbstractImpl impl;
103 public BGPNettyGroups() {
104 impl = Epoll.isAvailable() ? new EpollImpl() : new NioImpl();
110 public void close() {
112 impl.workerGroup.shutdownGracefully(0, TIMEOUT, TimeUnit.SECONDS);
113 impl.bossGroup.shutdownGracefully(0, TIMEOUT, TimeUnit.SECONDS);
118 Bootstrap createBootstrap(final @Nullable KeyMapping keys) {
119 final var bootstrap = new Bootstrap();
120 impl.setupBootstrap(bootstrap);
121 if (keys != null && !keys.isEmpty()) {
122 impl.setupKeys(bootstrap, keys);
124 return bootstrap.group(impl.workerGroup);
127 ServerBootstrap createServerBootstrap() {
128 final var bootstrap = new ServerBootstrap();
129 impl.setupBootstrap(bootstrap);
130 return bootstrap.group(impl.bossGroup, impl.workerGroup);