b75973092ea273e996d1644b5f0bb1bb1e48007b
[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(service = { }, 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     private Registration reg;
56
57     @Activate
58     public NettyReplicationSink(@Reference final BootstrapSupport bootstrapSupport,
59             @Reference final DOMDataBroker dataBroker,
60             @Reference final ClusterSingletonServiceProvider singletonService, final Config config)
61                 throws UnknownHostException {
62         reg = createSink(bootstrapSupport, dataBroker, singletonService, config.enabled(),
63             InetAddress.getByName(config.sourceHost()),
64             config.sourcePort(), Duration.ofMillis(config.reconnectDelayMillis()),
65             Duration.ofSeconds(config.keepAliveIntervalSeconds()), config.maxMissedKeepalives());
66     }
67
68     @Deactivate
69     void deactivate() {
70         reg.close();
71         reg = null;
72     }
73
74     static Registration createSink(final BootstrapSupport bootstrap, final DOMDataBroker broker,
75             final ClusterSingletonServiceProvider singleton, final boolean enabled,
76             final InetAddress sourceAddress, final int sourcePort, final Duration reconnectDelay,
77             final Duration keepaliveInterval, final int maxMissedKeepalives) {
78         LOG.debug("Sink {}", enabled ? "enabled" : "disabled");
79         checkArgument(maxMissedKeepalives > 0, "max-missed-keepalives %s must be greater than 0", maxMissedKeepalives);
80         return enabled ? singleton.registerClusterSingletonService(new SinkSingletonService(bootstrap,
81                 broker, new InetSocketAddress(sourceAddress, sourcePort), reconnectDelay, keepaliveInterval,
82                 maxMissedKeepalives)) : new NoOpRegistration();
83     }
84 }