e2967c369cdb88630d67a8b1d971a2b66067055c
[mdsal.git] / replicate / mdsal-replicate-netty / src / main / java / org / opendaylight / mdsal / replicate / netty / NettyReplicationSink.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
12 import java.net.InetAddress;
13 import java.net.InetSocketAddress;
14 import java.net.UnknownHostException;
15 import java.time.Duration;
16 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
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.sink")
30 @Designate(ocd = NettyReplicationSink.Config.class)
31 public final class NettyReplicationSink {
32     private static final Logger LOG = LoggerFactory.getLogger(NettyReplicationSink.class);
33
34     @ObjectClassDefinition
35     public @interface Config {
36         @AttributeDefinition(name = "enabled")
37         boolean enabled() default false;
38
39         @AttributeDefinition(name = "source-host")
40         String sourceHost() default "127.0.0.1";
41
42         @AttributeDefinition(name = "source-port")
43         int sourcePort() default 9999;
44
45         @AttributeDefinition(name = "reconnect-delay-millis")
46         int reconnectDelayMillis() default 3000;
47
48         @AttributeDefinition(name = "keepalive-interval-seconds")
49         int keepAliveIntervalSeconds() default 10;
50
51         @AttributeDefinition(name = "max-missed-keepalives")
52         int maxMissedKeepalives() default 5;
53     }
54
55     @Reference
56     private BootstrapSupport bootstrapSupport;
57
58     @Reference
59     private DOMDataBroker dataBroker;
60
61     @Reference
62     private ClusterSingletonServiceProvider singletonService;
63
64     private Registration reg;
65
66     public NettyReplicationSink() {
67         // Visible for DI
68     }
69
70     @Activate
71     void activate(final Config config) throws UnknownHostException {
72         final InetAddress sourceAddress = InetAddress.getByName(config.sourceHost());
73         final Duration reconnectDelay = Duration.ofMillis(config.reconnectDelayMillis());
74         final Duration keepaliveInterval = Duration.ofSeconds(config.keepAliveIntervalSeconds());
75
76         reg = createSink(bootstrapSupport, dataBroker, singletonService, config.enabled(), sourceAddress,
77                 config.sourcePort(), reconnectDelay, keepaliveInterval, config.maxMissedKeepalives());
78     }
79
80     @Deactivate
81     void deactivate() {
82         reg.close();
83         reg = null;
84     }
85
86     static Registration createSink(final BootstrapSupport bootstrap, final DOMDataBroker broker,
87                                   final ClusterSingletonServiceProvider singleton, final boolean enabled,
88                                   final InetAddress sourceAddress, final int sourcePort, final Duration reconnectDelay,
89                                   final Duration keepaliveInterval, final int maxMissedKeepalives) {
90         LOG.debug("Sink {}", enabled ? "enabled" : "disabled");
91         checkArgument(maxMissedKeepalives > 0, "max-missed-keepalives %s must be greater than 0", maxMissedKeepalives);
92         return enabled ? singleton.registerClusterSingletonService(new SinkSingletonService(bootstrap,
93                 broker, new InetSocketAddress(sourceAddress, sourcePort), reconnectDelay, keepaliveInterval,
94                 maxMissedKeepalives)) : new NoOpRegistration();
95     }
96 }