Disconnect bgp-rib-impl from global event loop groups
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPNettyGroups.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. 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.bgp.rib.impl;
9
10 import static java.util.Objects.requireNonNull;
11
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;
33
34 @Singleton
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;
40
41         AbstractImpl(final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) {
42             this.bossGroup = requireNonNull(bossGroup);
43             this.workerGroup = requireNonNull(workerGroup);
44         }
45
46         abstract void setupBootstrap(Bootstrap bootstrap);
47
48         abstract void setupBootstrap(ServerBootstrap serverBootstrap);
49
50         abstract void setupKeys(Bootstrap bootstrap, KeyMapping keys);
51     }
52
53     private static final class EpollImpl extends AbstractImpl {
54         EpollImpl() {
55             super(new EpollEventLoopGroup(), new EpollEventLoopGroup());
56         }
57
58         @Override
59         void setupBootstrap(final Bootstrap bootstrap) {
60             bootstrap.channel(EpollSocketChannel.class);
61             bootstrap.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
62         }
63
64         @Override
65         void setupBootstrap(final ServerBootstrap serverBootstrap) {
66             serverBootstrap.channel(EpollServerSocketChannel.class);
67             serverBootstrap.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
68         }
69
70         @Override
71         void setupKeys(final Bootstrap bootstrap, final KeyMapping keys) {
72             bootstrap.option(EpollChannelOption.TCP_MD5SIG, keys.asMap());
73         }
74     }
75
76     private static final class NioImpl extends AbstractImpl {
77         NioImpl() {
78             super(new NioEventLoopGroup(), new NioEventLoopGroup());
79         }
80
81         @Override
82         void setupBootstrap(final Bootstrap bootstrap) {
83             bootstrap.channel(NioSocketChannel.class);
84         }
85
86         @Override
87         void setupBootstrap(final ServerBootstrap serverBootstrap) {
88             serverBootstrap.channel(NioServerSocketChannel.class);
89         }
90
91         @Override
92         void setupKeys(final Bootstrap bootstrap, final KeyMapping keys) {
93             throw new UnsupportedOperationException(Epoll.unavailabilityCause().getCause());
94         }
95     }
96
97     private static final long TIMEOUT = 10;
98
99     private AbstractImpl impl;
100
101     @Inject
102     @Activate
103     public BGPNettyGroups() {
104         impl = Epoll.isAvailable() ? new EpollImpl() : new NioImpl();
105     }
106
107     @PreDestroy
108     @Deactivate
109     @Override
110     public void close() {
111         if (impl != null) {
112             impl.workerGroup.shutdownGracefully(0, TIMEOUT, TimeUnit.SECONDS);
113             impl.bossGroup.shutdownGracefully(0, TIMEOUT, TimeUnit.SECONDS);
114             impl = null;
115         }
116     }
117
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);
123         }
124         return bootstrap.group(impl.workerGroup);
125     }
126
127     ServerBootstrap createServerBootstrap() {
128         final var bootstrap = new ServerBootstrap();
129         impl.setupBootstrap(bootstrap);
130         return bootstrap.group(impl.bossGroup, impl.workerGroup);
131     }
132 }