Add transport-{api,tcp}
[netconf.git] / transport / transport-tcp / src / main / java / org / opendaylight / netconf / transport / tcp / EpollNettyImpl.java
1 /*
2  * Copyright (c) 2022 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.netconf.transport.tcp;
9
10 import io.netty.bootstrap.Bootstrap;
11 import io.netty.bootstrap.ServerBootstrap;
12 import io.netty.channel.ChannelOption;
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.channel.epoll.EpollChannelOption;
15 import io.netty.channel.epoll.EpollEventLoopGroup;
16 import io.netty.channel.epoll.EpollServerSocketChannel;
17 import io.netty.channel.epoll.EpollSocketChannel;
18 import java.util.concurrent.ThreadFactory;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.tcp.common.rev220524.tcp.common.grouping.Keepalives;
21
22 @NonNullByDefault
23 final class EpollNettyImpl extends AbstractNettyImpl {
24     @Override
25     Class<EpollSocketChannel> channelClass() {
26         return EpollSocketChannel.class;
27     }
28
29     @Override
30     Class<EpollServerSocketChannel> serverChannelClass() {
31         return EpollServerSocketChannel.class;
32     }
33
34     @Override
35     EventLoopGroup newEventLoopGroup(final int numThreads, final ThreadFactory threadFactory) {
36         return new EpollEventLoopGroup(numThreads, threadFactory);
37     }
38
39     @Override
40     boolean supportsKeepalives() {
41         return true;
42     }
43
44     @Override
45     void configureKeepalives(final Bootstrap bootstrap, final Keepalives keepalives) {
46         bootstrap
47             .option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE)
48             .option(EpollChannelOption.TCP_KEEPIDLE, keepalives.requireIdleTime().toJava())
49             .option(EpollChannelOption.TCP_KEEPCNT, keepalives.requireMaxProbes().toJava())
50             .option(EpollChannelOption.TCP_KEEPINTVL, keepalives.requireProbeInterval().toJava());
51     }
52
53     @Override
54     void configureKeepalives(final ServerBootstrap bootstrap, final Keepalives keepalives) {
55         bootstrap
56             .childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE)
57             .childOption(EpollChannelOption.TCP_KEEPIDLE, keepalives.requireIdleTime().toJava())
58             .childOption(EpollChannelOption.TCP_KEEPCNT, keepalives.requireMaxProbes().toJava())
59             .childOption(EpollChannelOption.TCP_KEEPINTVL, keepalives.requireProbeInterval().toJava());
60     }
61
62     @Override
63     public String toString() {
64         return "epoll(2)";
65     }
66 }