Convert NettyBootstrapFactory into a Component 07/104407/6
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 15 Feb 2023 15:46:58 +0000 (16:46 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 16 Feb 2023 06:52:58 +0000 (07:52 +0100)
This is a rather simple component, split it into a service and inject
it as a service.

Change-Id: I1bd81387b959b3324b4685b419665dde9ce92165
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
library/impl/pom.xml
library/impl/src/main/java/org/opendaylight/ovsdb/lib/impl/NettyBootstrapFactory.java
library/impl/src/main/java/org/opendaylight/ovsdb/lib/impl/NettyBootstrapFactoryImpl.java [new file with mode: 0644]
library/impl/src/main/resources/OSGI-INF/blueprint/library.xml

index b8cd95f77daf10b2be9b383a156fd2d08b68433c..8e7d25d7233fb24561224245df658523c8a6e969 100644 (file)
@@ -97,6 +97,10 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
       <artifactId>jakarta.annotation-api</artifactId>
       <optional>true</optional>
     </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.service.component.annotations</artifactId>
+    </dependency>
 
     <!-- Testing Dependencies -->
     <dependency>
index 1d2195787ffc111120ce5d0ef367f4933e103c64..2128e34ad5fbb4eb066bb8e501ba8a91d4ef46eb 100644 (file)
  */
 package org.opendaylight.ovsdb.lib.impl;
 
-import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import io.netty.bootstrap.Bootstrap;
 import io.netty.bootstrap.ServerBootstrap;
-import io.netty.channel.AdaptiveRecvByteBufAllocator;
-import io.netty.channel.ChannelOption;
-import io.netty.channel.EventLoopGroup;
-import io.netty.channel.epoll.Epoll;
-import io.netty.channel.epoll.EpollEventLoopGroup;
-import io.netty.channel.epoll.EpollServerSocketChannel;
-import io.netty.channel.epoll.EpollSocketChannel;
-import io.netty.channel.nio.NioEventLoopGroup;
-import io.netty.channel.socket.nio.NioServerSocketChannel;
-import io.netty.channel.socket.nio.NioSocketChannel;
-import java.util.concurrent.ThreadFactory;
-import javax.annotation.PreDestroy;
-import javax.inject.Inject;
-import javax.inject.Singleton;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
-/**
- * A globally-instantiated context for use with OvsdbConnectionService.
- */
-@Singleton
-public class NettyBootstrapFactory implements AutoCloseable {
-    private abstract static class Provider {
-        /**
-         * Return user friendly name, suitable for system operators.
-         *
-         * @return An admin-friendly name.
-         */
-        abstract String name();
-
-        abstract EventLoopGroup createGroup(ThreadFactory threadFactory);
-
-        abstract Bootstrap createBootstrap();
-
-        abstract ServerBootstrap createServerBootstrap();
-    }
-
-    private static final class EpollProvider extends Provider {
-        @Override
-        String name() {
-            return "epoll(7)";
-        }
-
-        @Override
-        EventLoopGroup createGroup(final ThreadFactory threadFactory) {
-            return new EpollEventLoopGroup(0, threadFactory);
-        }
-
-        @Override
-        Bootstrap createBootstrap() {
-            return new Bootstrap()
-                    .channel(EpollSocketChannel.class);
-        }
-
-        @Override
-        ServerBootstrap createServerBootstrap() {
-            return new ServerBootstrap()
-                    .channel(EpollServerSocketChannel.class);
-        }
-    }
-
-    private static final class NioProvider extends Provider {
-        @Override
-        String name() {
-            return "java.nio";
-        }
-
-        @Override
-        EventLoopGroup createGroup(final ThreadFactory threadFactory) {
-            return new NioEventLoopGroup(0, threadFactory);
-        }
-
-        @Override
-        Bootstrap createBootstrap() {
-            return new Bootstrap()
-                    .channel(NioSocketChannel.class);
-        }
-
-        @Override
-        ServerBootstrap createServerBootstrap() {
-            return new ServerBootstrap()
-                    .channel(NioServerSocketChannel.class);
-        }
-    }
-
-    private static final Logger LOG = LoggerFactory.getLogger(NettyBootstrapFactory.class);
-
-    // Minimum footprint runtime-constant
-    private static final Provider PROVIDER = Epoll.isAvailable() ? new EpollProvider() : new NioProvider();
-
-    private final EventLoopGroup bossGroup;
-    private final EventLoopGroup workerGroup;
-
-    @Inject
-    public NettyBootstrapFactory() {
-        bossGroup = PROVIDER.createGroup(new ThreadFactoryBuilder().setNameFormat("OVSDB listener-%d").build());
-        workerGroup = PROVIDER.createGroup(new ThreadFactoryBuilder().setNameFormat("OVSDB connection-%d").build());
-        LOG.info("OVSDB global Netty context started with {}", PROVIDER.name());
-    }
-
-    Bootstrap newClient() {
-        return PROVIDER.createBootstrap()
-                .group(workerGroup)
-                .option(ChannelOption.TCP_NODELAY, true)
-                .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(65535, 65535, 65535));
-    }
+public interface NettyBootstrapFactory {
 
-    ServerBootstrap newServer() {
-        return PROVIDER.createServerBootstrap()
-                .group(bossGroup, workerGroup)
-                .option(ChannelOption.TCP_NODELAY, true)
-                .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(65535, 65535, 65535))
-                .option(ChannelOption.SO_BACKLOG, 100);
-    }
+    Bootstrap newClient();
 
-    @PreDestroy
-    @Override
-    public void close() {
-        LOG.info("OVSDB global Netty context terminating");
-        bossGroup.shutdownGracefully().addListener(ignore -> {
-            LOG.info("OVSDB global server group terminated");
-        });
-        workerGroup.shutdownGracefully().addListener(ignore -> {
-            LOG.info("OVSDB global channel group terminated");
-        });
-    }
+    ServerBootstrap newServer();
 }
diff --git a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/impl/NettyBootstrapFactoryImpl.java b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/impl/NettyBootstrapFactoryImpl.java
new file mode 100644 (file)
index 0000000..b610cd9
--- /dev/null
@@ -0,0 +1,147 @@
+/*
+ * Copyright © 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.ovsdb.lib.impl;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import io.netty.bootstrap.Bootstrap;
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.AdaptiveRecvByteBufAllocator;
+import io.netty.channel.ChannelOption;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.epoll.Epoll;
+import io.netty.channel.epoll.EpollEventLoopGroup;
+import io.netty.channel.epoll.EpollServerSocketChannel;
+import io.netty.channel.epoll.EpollSocketChannel;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import java.util.concurrent.ThreadFactory;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A globally-instantiated context for use with OvsdbConnectionService.
+ */
+@Singleton
+@Component(service = NettyBootstrapFactory.class)
+public final class NettyBootstrapFactoryImpl implements NettyBootstrapFactory, AutoCloseable {
+    private abstract static class Provider {
+        /**
+         * Return user friendly name, suitable for system operators.
+         *
+         * @return An admin-friendly name.
+         */
+        abstract String name();
+
+        abstract EventLoopGroup createGroup(ThreadFactory threadFactory);
+
+        abstract Bootstrap createBootstrap();
+
+        abstract ServerBootstrap createServerBootstrap();
+    }
+
+    private static final class EpollProvider extends Provider {
+        @Override
+        String name() {
+            return "epoll(7)";
+        }
+
+        @Override
+        EventLoopGroup createGroup(final ThreadFactory threadFactory) {
+            return new EpollEventLoopGroup(0, threadFactory);
+        }
+
+        @Override
+        Bootstrap createBootstrap() {
+            return new Bootstrap()
+                    .channel(EpollSocketChannel.class);
+        }
+
+        @Override
+        ServerBootstrap createServerBootstrap() {
+            return new ServerBootstrap()
+                    .channel(EpollServerSocketChannel.class);
+        }
+    }
+
+    private static final class NioProvider extends Provider {
+        @Override
+        String name() {
+            return "java.nio";
+        }
+
+        @Override
+        EventLoopGroup createGroup(final ThreadFactory threadFactory) {
+            return new NioEventLoopGroup(0, threadFactory);
+        }
+
+        @Override
+        Bootstrap createBootstrap() {
+            return new Bootstrap()
+                    .channel(NioSocketChannel.class);
+        }
+
+        @Override
+        ServerBootstrap createServerBootstrap() {
+            return new ServerBootstrap()
+                    .channel(NioServerSocketChannel.class);
+        }
+    }
+
+    private static final Logger LOG = LoggerFactory.getLogger(NettyBootstrapFactoryImpl.class);
+
+    // Minimum footprint runtime-constant
+    private static final Provider PROVIDER = Epoll.isAvailable() ? new EpollProvider() : new NioProvider();
+
+    private final EventLoopGroup bossGroup;
+    private final EventLoopGroup workerGroup;
+
+    @Inject
+    @Activate
+    public NettyBootstrapFactoryImpl() {
+        bossGroup = PROVIDER.createGroup(new ThreadFactoryBuilder().setNameFormat("OVSDB listener-%d").build());
+        workerGroup = PROVIDER.createGroup(new ThreadFactoryBuilder().setNameFormat("OVSDB connection-%d").build());
+        LOG.info("OVSDB global Netty context started with {}", PROVIDER.name());
+    }
+
+    @PreDestroy
+    @Deactivate
+    @Override
+    public void close() {
+        LOG.info("OVSDB global Netty context terminating");
+        bossGroup.shutdownGracefully().addListener(ignore -> {
+            LOG.info("OVSDB global server group terminated");
+        });
+        workerGroup.shutdownGracefully().addListener(ignore -> {
+            LOG.info("OVSDB global channel group terminated");
+        });
+    }
+
+    @Override
+    public Bootstrap newClient() {
+        return PROVIDER.createBootstrap()
+                .group(workerGroup)
+                .option(ChannelOption.TCP_NODELAY, true)
+                .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(65535, 65535, 65535));
+    }
+
+    @Override
+    public ServerBootstrap newServer() {
+        return PROVIDER.createServerBootstrap()
+                .group(bossGroup, workerGroup)
+                .option(ChannelOption.TCP_NODELAY, true)
+                .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(65535, 65535, 65535))
+                .option(ChannelOption.SO_BACKLOG, 100);
+    }
+}
index 42b649c2315daaac09114c7a98102634bfc932e0..9a955e7e2f010cb6329c330a4f4d136d9672bb55 100644 (file)
@@ -31,8 +31,8 @@
 
   <reference id="certificateManager" interface="org.opendaylight.aaa.cert.api.ICertificateManager"
              filter="type=default-certificate-manager"/>
+  <reference id="nettyBootstrapFactory" interface="org.opendaylight.ovsdb.lib.impl.NettyBootstrapFactory"/>
 
-  <bean id="nettyBootstrapFactory" class="org.opendaylight.ovsdb.lib.impl.NettyBootstrapFactory" destroy-method="close"/>
   <bean id="ovsdbConnectionService" class="org.opendaylight.ovsdb.lib.impl.OvsdbConnectionService">
     <argument ref="nettyBootstrapFactory"/>
     <argument ref="certificateManager"/>