924307d1d31305f51f186bbe648c15f9d786f74a
[mdsal.git] / replicate / mdsal-replicate-netty / src / main / java / org / opendaylight / mdsal / replicate / netty / NettyReplicationSource.java
1 /*
2  * Copyright (c) 2020 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.mdsal.replicate.netty;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12
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;
28
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);
33
34     @ObjectClassDefinition
35     public @interface Config {
36         @AttributeDefinition(name = "enabled")
37         boolean enabled() default false;
38
39         @AttributeDefinition(name = "listen-port")
40         int listenPort() default 9999;
41
42         @AttributeDefinition(name = "keepalive-interval-seconds")
43         int keepAliveIntervalSeconds() default 10;
44
45         @AttributeDefinition(name = "max-missed-keepalives")
46         int maxMissedKeepalives() default 5;
47     }
48
49     @Reference
50     private BootstrapSupport bootstrapSupport;
51
52     @Reference
53     private DOMDataBroker dataBroker;
54
55     @Reference
56     private ClusterSingletonServiceProvider singletonService;
57
58     private Registration reg;
59
60     public NettyReplicationSource() {
61         // Visible for DI
62     }
63
64     @Activate
65     void activate(final Config config) {
66         final Duration keepaliveInterval = Duration.ofSeconds(config.keepAliveIntervalSeconds());
67
68         reg = createSource(bootstrapSupport, dataBroker, singletonService, config.enabled(), config.listenPort(),
69                 keepaliveInterval, config.maxMissedKeepalives());
70     }
71
72     @Deactivate
73     void deactivate() {
74         reg.close();
75         reg = null;
76     }
77
78     @VisibleForTesting
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();
89     }
90 }