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;
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;
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);
34 @ObjectClassDefinition
35 public @interface Config {
36 @AttributeDefinition(name = "enabled")
37 boolean enabled() default false;
39 @AttributeDefinition(name = "source-host")
40 String sourceHost() default "127.0.0.1";
42 @AttributeDefinition(name = "source-port")
43 int sourcePort() default 9999;
45 @AttributeDefinition(name = "reconnect-delay-millis")
46 int reconnectDelayMillis() default 3000;
48 @AttributeDefinition(name = "keepalive-interval-seconds")
49 int keepAliveIntervalSeconds() default 10;
51 @AttributeDefinition(name = "max-missed-keepalives")
52 int maxMissedKeepalives() default 5;
56 private BootstrapSupport bootstrapSupport;
59 private DOMDataBroker dataBroker;
62 private ClusterSingletonServiceProvider singletonService;
64 private Registration reg;
66 public NettyReplicationSink() {
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());
76 reg = createSink(bootstrapSupport, dataBroker, singletonService, config.enabled(), sourceAddress,
77 config.sourcePort(), reconnectDelay, keepaliveInterval, config.maxMissedKeepalives());
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();