Netty Replicator - improve the reconnection and keepalive mechanisms
[mdsal.git] / replicate / mdsal-replicate-netty / src / main / java / org / opendaylight / mdsal / replicate / netty / NettyReplication.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 java.net.InetAddress;
14 import java.net.InetSocketAddress;
15 import java.time.Duration;
16 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
19 import org.opendaylight.yangtools.concepts.AbstractRegistration;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public final class NettyReplication {
25     private static final Logger LOG = LoggerFactory.getLogger(NettyReplication.class);
26
27     private static final class Disabled extends AbstractRegistration {
28         @Override
29         protected void removeRegistration() {
30             // no-op
31         }
32     }
33
34     private NettyReplication() {
35         // Hidden on purpose
36     }
37
38     public static Registration createSink(final BootstrapSupport bootstrapSupport, final DOMDataBroker dataBroker,
39             final ClusterSingletonServiceProvider singletonService, final boolean enabled,
40             final InetAddress sourceAddress, final int sourcePort, final Duration reconnectDelay,
41         final Duration keepaliveInterval, final int maxMissedKeepalives) {
42         LOG.debug("Sink {}", enabled ? "enabled" : "disabled");
43         checkArgument(maxMissedKeepalives > 0, "max-missed-keepalives %s must be greater than 0", maxMissedKeepalives);
44         return enabled ? singletonService.registerClusterSingletonService(new SinkSingletonService(bootstrapSupport,
45             dataBroker, new InetSocketAddress(sourceAddress, sourcePort), reconnectDelay, keepaliveInterval,
46             maxMissedKeepalives)) : new Disabled();
47     }
48
49     public static Registration createSource(final BootstrapSupport bootstrapSupport, final DOMDataBroker dataBroker,
50             final ClusterSingletonServiceProvider singletonService, final boolean enabled, final int listenPort,
51         final Duration keepaliveInterval, final int maxMissedKeepalives) {
52         LOG.debug("Source {}", enabled ? "enabled" : "disabled");
53         final DOMDataTreeChangeService dtcs = dataBroker.getExtensions().getInstance(DOMDataTreeChangeService.class);
54         verify(dtcs != null, "Missing DOMDataTreeChangeService in broker %s", dataBroker);
55         checkArgument(maxMissedKeepalives > 0, "max-missed-keepalives %s must be greater than 0", maxMissedKeepalives);
56         return enabled ? singletonService.registerClusterSingletonService(new SourceSingletonService(bootstrapSupport,
57             dtcs, listenPort, keepaliveInterval, maxMissedKeepalives)) : new Disabled();
58     }
59 }