Fix findbugs warnings
[bgpcep.git] / bgp / rib-spi / src / main / java / org / opendaylight / protocol / bgp / rib / spi / util / ClusterSingletonServiceRegistrationHelper.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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
9 package org.opendaylight.protocol.bgp.rib.spi.util;
10
11 import com.google.common.util.concurrent.Uninterruptibles;
12 import java.util.concurrent.TimeUnit;
13 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
14 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
15 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Utility class which provides helper functionality for ClusterSingletonService.
21  */
22 public final class ClusterSingletonServiceRegistrationHelper {
23
24     private static final Logger LOG = LoggerFactory.getLogger(ClusterSingletonServiceRegistrationHelper.class);
25     private static final int MAX_REGISTRATION_ATTEMPTS = 10;
26     private static final int SLEEP_TIME_MILLIS = MAX_REGISTRATION_ATTEMPTS;
27
28     private ClusterSingletonServiceRegistrationHelper() {
29         throw new UnsupportedOperationException();
30     }
31
32     /**
33      * This helper function wraps
34      * {@link ClusterSingletonServiceProvider#registerClusterSingletonService(ClusterSingletonService)} in order to
35      * execute repeated registration attempts while catching RuntimeException. If registration is not successful,
36      * RuntimeException is re-thrown.
37      *
38      * @param singletonProvider Cluster Singleton Service Provider
39      * @param clusterSingletonService Cluster Singleton Service
40      * @param maxAttempts             Upper bound for registration retries count.
41      * @param sleepTime               Sleep time between registration retries in milliseconds.
42      * @return Registration
43      */
44     @SuppressWarnings("checkstyle:IllegalCatch")
45     public static ClusterSingletonServiceRegistration registerSingletonService(
46         final ClusterSingletonServiceProvider singletonProvider,
47         final ClusterSingletonService clusterSingletonService, final int maxAttempts, final int sleepTime) {
48         int attempts = maxAttempts;
49         while (true) {
50             try {
51                 return singletonProvider.registerClusterSingletonService(clusterSingletonService);
52             } catch (final RuntimeException e) {
53                 if (attempts == 0) {
54                     LOG.error("Giving up after {} registration attempts for service {}.", maxAttempts,
55                         clusterSingletonService, e);
56                     throw e;
57                 }
58                 attempts--;
59                 LOG.warn("Failed to register {} service to ClusterSingletonServiceProvider. Try again in {} ms.",
60                     clusterSingletonService, sleepTime, e);
61                 Uninterruptibles.sleepUninterruptibly(sleepTime, TimeUnit.MILLISECONDS);
62             }
63         }
64     }
65
66     /**
67      * This helper function wraps
68      * {@link ClusterSingletonServiceProvider#registerClusterSingletonService(ClusterSingletonService)} in order to
69      * execute repeated registration attempts while catching RuntimeException. If registration is not successful,
70      * RuntimeException is re-thrown. 10 registration attempts will be tried with 10 ms pause between each
71      * other.
72      *
73      * @param singletonProvider Cluster Singleton Service Provider
74      * @param clusterSingletonService Cluster Singleton Service
75      * @return Registration
76      */
77     public static ClusterSingletonServiceRegistration registerSingletonService(final ClusterSingletonServiceProvider
78         singletonProvider, final ClusterSingletonService clusterSingletonService) {
79         return ClusterSingletonServiceRegistrationHelper.registerSingletonService(singletonProvider,
80             clusterSingletonService, MAX_REGISTRATION_ATTEMPTS, SLEEP_TIME_MILLIS);
81     }
82 }