Fix checkstyle violations in clustering-it-provider
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / FlappingSingletonService.java
1 /*
2  * Copyright (c) 2017 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.controller.clustering.it.provider.impl;
10
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.concurrent.ScheduledExecutorService;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
17 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
19 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class FlappingSingletonService implements ClusterSingletonService {
24
25     private static final Logger LOG = LoggerFactory.getLogger(FlappingSingletonService.class);
26
27     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
28             ServiceGroupIdentifier.create("flapping-singleton-service");
29
30     private static final ScheduledExecutorService EXECUTOR = FinalizableScheduledExecutorService.newSingleThread();
31
32     private final ClusterSingletonServiceProvider singletonServiceProvider;
33     private final AtomicBoolean active = new AtomicBoolean(true);
34
35     private volatile long flapCount = 0;
36     private volatile ClusterSingletonServiceRegistration registration;
37
38     public FlappingSingletonService(final ClusterSingletonServiceProvider singletonServiceProvider) {
39         LOG.debug("Registering flapping-singleton-service.");
40
41         this.singletonServiceProvider = singletonServiceProvider;
42         registration = singletonServiceProvider.registerClusterSingletonService(this);
43     }
44
45     @Override
46     @SuppressWarnings("checkstyle:IllegalCatch")
47     public void instantiateServiceInstance() {
48         LOG.debug("Instantiating flapping-singleton-service.");
49
50         // TODO direct registration/close seem to trigger a bug in singleton state transitions,
51         // remove the whole executor shenanigans after it's fixed.
52         EXECUTOR.submit(() -> {
53             try {
54                 registration.close();
55                 registration = null;
56             } catch (Exception e) {
57                 LOG.warn("There was a problem closing flapping singleton service.", e);
58                 setInactive();
59                 flapCount = -flapCount;
60             }
61         });
62     }
63
64     @Override
65     @SuppressWarnings("checkstyle:IllegalCatch")
66     public ListenableFuture<Void> closeServiceInstance() {
67         LOG.debug("Closing flapping-singleton-service, flapCount: {}", flapCount);
68
69         flapCount++;
70         if (active.get()) {
71             // TODO direct registration/close seem to trigger a bug in singleton state transitions,
72             // remove  whole executor shenanigans after it's fixed.
73             // Needs to be delayed slightly otherwise it's triggered as well.
74             EXECUTOR.schedule(() -> {
75                 LOG.debug("Running re-registration");
76                 try {
77                     registration = singletonServiceProvider.registerClusterSingletonService(this);
78                 } catch (RuntimeException e) {
79                     LOG.warn("There was a problem re-registering flapping singleton service.", e);
80                     setInactive();
81                     flapCount = -flapCount - 1;
82                 }
83
84             }, 200, TimeUnit.MILLISECONDS);
85         }
86
87         return Futures.immediateFuture(null);
88     }
89
90     @Override
91     public ServiceGroupIdentifier getIdentifier() {
92         return SERVICE_GROUP_IDENTIFIER;
93     }
94
95     public long setInactive() {
96         LOG.debug("Setting flapping-singleton-service to inactive, flap-count: {}", flapCount);
97
98         active.set(false);
99         return flapCount;
100     }
101 }