package org.opendaylight.mdsal.replicate.netty;
import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Verify.verify;
+import static com.google.common.base.Verify.verifyNotNull;
+import com.google.common.annotations.VisibleForTesting;
import java.time.Duration;
import org.opendaylight.mdsal.dom.api.DOMDataBroker;
-import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
+import org.opendaylight.mdsal.dom.api.DOMDataBroker.DataTreeChangeExtension;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-@Component(immediate = true, configurationPid = "org.opendaylight.mdsal.replicate.netty.source")
+@Component(service = { }, configurationPid = "org.opendaylight.mdsal.replicate.netty.source")
@Designate(ocd = NettyReplicationSource.Config.class)
public final class NettyReplicationSource {
private static final Logger LOG = LoggerFactory.getLogger(NettyReplicationSource.class);
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 NettyReplicationSource() {
-
- }
+ private Registration reg;
@Activate
- void activate(final Config config) {
- final Duration keepaliveInterval = Duration.ofSeconds(config.keepAliveIntervalSeconds());
+ public NettyReplicationSource(@Reference final BootstrapSupport bootstrapSupport,
+ @Reference final DOMDataBroker dataBroker,
+ @Reference final ClusterSingletonServiceProvider singletonService, final Config config) {
+ reg = createSource(bootstrapSupport, dataBroker, singletonService, config.enabled(), config.listenPort(),
+ Duration.ofSeconds(config.keepAliveIntervalSeconds()), config.maxMissedKeepalives());
+ }
- createSource(bootstrapSupport, dataBroker, singletonService, config.enabled(), config.listenPort(),
- keepaliveInterval, config.maxMissedKeepalives());
+ @Deactivate
+ void deactivate() {
+ reg.close();
+ reg = null;
}
+ @VisibleForTesting
static Registration createSource(final BootstrapSupport bootstrap, final DOMDataBroker broker,
- final ClusterSingletonServiceProvider singleton, final boolean enabled,
- final int listenPort, final Duration keepaliveInterval,
- final int maxMissedKeepalives) {
+ final ClusterSingletonServiceProvider singleton, final boolean enabled, final int listenPort,
+ final Duration keepaliveInterval, final int maxMissedKeepalives) {
LOG.debug("Source {}", enabled ? "enabled" : "disabled");
- final DOMDataTreeChangeService dtcs = broker.getExtensions().getInstance(DOMDataTreeChangeService.class);
- verify(dtcs != null, "Missing DOMDataTreeChangeService in broker %s", broker);
+ final var dtcs = verifyNotNull(broker.extension(DataTreeChangeExtension.class),
+ "Missing DOMDataTreeChangeService in broker %s", broker);
checkArgument(maxMissedKeepalives > 0, "max-missed-keepalives %s must be greater than 0", maxMissedKeepalives);
return enabled ? singleton.registerClusterSingletonService(new SourceSingletonService(bootstrap,
- dtcs, listenPort, keepaliveInterval, maxMissedKeepalives)) : new Disabled();
+ dtcs, listenPort, keepaliveInterval, maxMissedKeepalives)) : new NoOpRegistration();
}
}