2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.replicate.netty;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
13 import com.google.common.annotations.VisibleForTesting;
14 import java.time.Duration;
15 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
17 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
18 import org.opendaylight.yangtools.concepts.Registration;
19 import org.osgi.service.component.annotations.Activate;
20 import org.osgi.service.component.annotations.Component;
21 import org.osgi.service.component.annotations.Deactivate;
22 import org.osgi.service.component.annotations.Reference;
23 import org.osgi.service.metatype.annotations.AttributeDefinition;
24 import org.osgi.service.metatype.annotations.Designate;
25 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 @Component(immediate = true, configurationPid = "org.opendaylight.mdsal.replicate.netty.source")
30 @Designate(ocd = NettyReplicationSource.Config.class)
31 public final class NettyReplicationSource {
32 private static final Logger LOG = LoggerFactory.getLogger(NettyReplicationSource.class);
34 @ObjectClassDefinition
35 public @interface Config {
36 @AttributeDefinition(name = "enabled")
37 boolean enabled() default false;
39 @AttributeDefinition(name = "listen-port")
40 int listenPort() default 9999;
42 @AttributeDefinition(name = "keepalive-interval-seconds")
43 int keepAliveIntervalSeconds() default 10;
45 @AttributeDefinition(name = "max-missed-keepalives")
46 int maxMissedKeepalives() default 5;
50 private BootstrapSupport bootstrapSupport;
53 private DOMDataBroker dataBroker;
56 private ClusterSingletonServiceProvider singletonService;
58 private Registration reg;
60 public NettyReplicationSource() {
65 void activate(final Config config) {
66 final Duration keepaliveInterval = Duration.ofSeconds(config.keepAliveIntervalSeconds());
68 reg = createSource(bootstrapSupport, dataBroker, singletonService, config.enabled(), config.listenPort(),
69 keepaliveInterval, config.maxMissedKeepalives());
79 static Registration createSource(final BootstrapSupport bootstrap, final DOMDataBroker broker,
80 final ClusterSingletonServiceProvider singleton, final boolean enabled,
81 final int listenPort, final Duration keepaliveInterval,
82 final int maxMissedKeepalives) {
83 LOG.debug("Source {}", enabled ? "enabled" : "disabled");
84 final var dtcs = broker.extension(DOMDataTreeChangeService.class);
85 verify(dtcs != null, "Missing DOMDataTreeChangeService in broker %s", broker);
86 checkArgument(maxMissedKeepalives > 0, "max-missed-keepalives %s must be greater than 0", maxMissedKeepalives);
87 return enabled ? singleton.registerClusterSingletonService(new SourceSingletonService(bootstrap,
88 dtcs, listenPort, keepaliveInterval, maxMissedKeepalives)) : new NoOpRegistration();