Use constructor injection in mdsal-replicate-netty
[mdsal.git] / replicate / mdsal-replicate-netty / src / main / java / org / opendaylight / mdsal / replicate / netty / NettyReplicationSink.java
index 94d9005fa136559444ad00443993fdf7f7925fc4..b75973092ea273e996d1644b5f0bb1bb1e48007b 100644 (file)
@@ -15,10 +15,10 @@ import java.net.UnknownHostException;
 import java.time.Duration;
 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
-import org.opendaylight.yangtools.concepts.AbstractRegistration;
 import org.opendaylight.yangtools.concepts.Registration;
 import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
 import org.osgi.service.component.annotations.Reference;
 import org.osgi.service.metatype.annotations.AttributeDefinition;
 import org.osgi.service.metatype.annotations.Designate;
@@ -26,7 +26,7 @@ import org.osgi.service.metatype.annotations.ObjectClassDefinition;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@Component(immediate = true, configurationPid = "org.opendaylight.mdsal.replicate.netty.sink")
+@Component(service = { }, configurationPid = "org.opendaylight.mdsal.replicate.netty.sink")
 @Designate(ocd = NettyReplicationSink.Config.class)
 public final class NettyReplicationSink {
     private static final Logger LOG = LoggerFactory.getLogger(NettyReplicationSink.class);
@@ -52,44 +52,33 @@ public final class NettyReplicationSink {
         int maxMissedKeepalives() default 5;
     }
 
-    @Reference
-    private BootstrapSupport bootstrapSupport;
-
-    @Reference
-    private DOMDataBroker dataBroker;
-
-    @Reference
-    private ClusterSingletonServiceProvider singletonService;
-
-    private static final class Disabled extends AbstractRegistration {
-        @Override
-        protected void removeRegistration() {
-            // no-op
-        }
-    }
-
-    private NettyReplicationSink() {
-
-    }
+    private Registration reg;
 
     @Activate
-    void activate(final Config config) throws UnknownHostException {
-        final InetAddress sourceAddress = InetAddress.getByName(config.sourceHost());
-        final Duration reconnectDelay = Duration.ofMillis(config.reconnectDelayMillis());
-        final Duration keepaliveInterval = Duration.ofSeconds(config.keepAliveIntervalSeconds());
+    public NettyReplicationSink(@Reference final BootstrapSupport bootstrapSupport,
+            @Reference final DOMDataBroker dataBroker,
+            @Reference final ClusterSingletonServiceProvider singletonService, final Config config)
+                throws UnknownHostException {
+        reg = createSink(bootstrapSupport, dataBroker, singletonService, config.enabled(),
+            InetAddress.getByName(config.sourceHost()),
+            config.sourcePort(), Duration.ofMillis(config.reconnectDelayMillis()),
+            Duration.ofSeconds(config.keepAliveIntervalSeconds()), config.maxMissedKeepalives());
+    }
 
-        createSink(bootstrapSupport, dataBroker, singletonService, config.enabled(), sourceAddress,
-                config.sourcePort(), reconnectDelay, keepaliveInterval, config.maxMissedKeepalives());
+    @Deactivate
+    void deactivate() {
+        reg.close();
+        reg = null;
     }
 
     static Registration createSink(final BootstrapSupport bootstrap, final DOMDataBroker broker,
-                                  final ClusterSingletonServiceProvider singleton, final boolean enabled,
-                                  final InetAddress sourceAddress, final int sourcePort, final Duration reconnectDelay,
-                                  final Duration keepaliveInterval, final int maxMissedKeepalives) {
+            final ClusterSingletonServiceProvider singleton, final boolean enabled,
+            final InetAddress sourceAddress, final int sourcePort, final Duration reconnectDelay,
+            final Duration keepaliveInterval, final int maxMissedKeepalives) {
         LOG.debug("Sink {}", enabled ? "enabled" : "disabled");
         checkArgument(maxMissedKeepalives > 0, "max-missed-keepalives %s must be greater than 0", maxMissedKeepalives);
         return enabled ? singleton.registerClusterSingletonService(new SinkSingletonService(bootstrap,
                 broker, new InetSocketAddress(sourceAddress, sourcePort), reconnectDelay, keepaliveInterval,
-                maxMissedKeepalives)) : new Disabled();
+                maxMissedKeepalives)) : new NoOpRegistration();
     }
 }