X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=replicate%2Fmdsal-replicate-netty%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Freplicate%2Fnetty%2FNettyReplicationSink.java;fp=replicate%2Fmdsal-replicate-netty%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Freplicate%2Fnetty%2FNettyReplicationSink.java;h=94d9005fa136559444ad00443993fdf7f7925fc4;hb=7bcfd42b7843ff655435197d7f64584297de2633;hp=0000000000000000000000000000000000000000;hpb=58055cea3b68b59942423171fa4e0d92ba9ce65e;p=mdsal.git diff --git a/replicate/mdsal-replicate-netty/src/main/java/org/opendaylight/mdsal/replicate/netty/NettyReplicationSink.java b/replicate/mdsal-replicate-netty/src/main/java/org/opendaylight/mdsal/replicate/netty/NettyReplicationSink.java new file mode 100644 index 0000000000..94d9005fa1 --- /dev/null +++ b/replicate/mdsal-replicate-netty/src/main/java/org/opendaylight/mdsal/replicate/netty/NettyReplicationSink.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.mdsal.replicate.netty; + +import static com.google.common.base.Preconditions.checkArgument; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.UnknownHostException; +import java.time.Duration; +import org.opendaylight.mdsal.dom.api.DOMDataBroker; +import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider; +import org.opendaylight.yangtools.concepts.AbstractRegistration; +import org.opendaylight.yangtools.concepts.Registration; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.metatype.annotations.AttributeDefinition; +import org.osgi.service.metatype.annotations.Designate; +import org.osgi.service.metatype.annotations.ObjectClassDefinition; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Component(immediate = true, configurationPid = "org.opendaylight.mdsal.replicate.netty.sink") +@Designate(ocd = NettyReplicationSink.Config.class) +public final class NettyReplicationSink { + private static final Logger LOG = LoggerFactory.getLogger(NettyReplicationSink.class); + + @ObjectClassDefinition + public @interface Config { + @AttributeDefinition(name = "enabled") + boolean enabled() default false; + + @AttributeDefinition(name = "source-host") + String sourceHost() default "127.0.0.1"; + + @AttributeDefinition(name = "source-port") + int sourcePort() default 9999; + + @AttributeDefinition(name = "reconnect-delay-millis") + int reconnectDelayMillis() default 3000; + + @AttributeDefinition(name = "keepalive-interval-seconds") + int keepAliveIntervalSeconds() default 10; + + @AttributeDefinition(name = "max-missed-keepalives") + int maxMissedKeepalives() default 5; + } + + @Reference + private BootstrapSupport bootstrapSupport; + + @Reference + private DOMDataBroker dataBroker; + + @Reference + private ClusterSingletonServiceProvider singletonService; + + private static final class Disabled extends AbstractRegistration { + @Override + protected void removeRegistration() { + // no-op + } + } + + private NettyReplicationSink() { + + } + + @Activate + void activate(final Config config) throws UnknownHostException { + final InetAddress sourceAddress = InetAddress.getByName(config.sourceHost()); + final Duration reconnectDelay = Duration.ofMillis(config.reconnectDelayMillis()); + final Duration keepaliveInterval = Duration.ofSeconds(config.keepAliveIntervalSeconds()); + + createSink(bootstrapSupport, dataBroker, singletonService, config.enabled(), sourceAddress, + config.sourcePort(), reconnectDelay, keepaliveInterval, config.maxMissedKeepalives()); + } + + static Registration createSink(final BootstrapSupport bootstrap, final DOMDataBroker broker, + final ClusterSingletonServiceProvider singleton, final boolean enabled, + final InetAddress sourceAddress, final int sourcePort, final Duration reconnectDelay, + final Duration keepaliveInterval, final int maxMissedKeepalives) { + LOG.debug("Sink {}", enabled ? "enabled" : "disabled"); + checkArgument(maxMissedKeepalives > 0, "max-missed-keepalives %s must be greater than 0", maxMissedKeepalives); + return enabled ? singleton.registerClusterSingletonService(new SinkSingletonService(bootstrap, + broker, new InetSocketAddress(sourceAddress, sourcePort), reconnectDelay, keepaliveInterval, + maxMissedKeepalives)) : new Disabled(); + } +}