Fixup checkstyle
[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 package org.opendaylight.controller.clustering.it.provider.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.concurrent.atomic.AtomicBoolean;
15 import java.util.concurrent.atomic.AtomicLong;
16 import org.opendaylight.mdsal.singleton.api.ClusterSingletonService;
17 import org.opendaylight.mdsal.singleton.api.ClusterSingletonServiceProvider;
18 import org.opendaylight.mdsal.singleton.api.ServiceGroupIdentifier;
19 import org.opendaylight.yangtools.concepts.Registration;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public final class FlappingSingletonService implements ClusterSingletonService {
24     private static final Logger LOG = LoggerFactory.getLogger(FlappingSingletonService.class);
25     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
26             new ServiceGroupIdentifier("flapping-singleton-service");
27
28     private final ClusterSingletonServiceProvider singletonServiceProvider;
29     private final AtomicBoolean active = new AtomicBoolean(true);
30     private final AtomicLong flapCount = new AtomicLong();
31
32     private volatile Registration registration;
33
34     public FlappingSingletonService(final ClusterSingletonServiceProvider singletonServiceProvider) {
35         LOG.debug("Registering flapping-singleton-service.");
36         this.singletonServiceProvider = requireNonNull(singletonServiceProvider);
37         registration = singletonServiceProvider.registerClusterSingletonService(this);
38     }
39
40     @Override
41     @SuppressWarnings("checkstyle:IllegalCatch")
42     public void instantiateServiceInstance() {
43         LOG.debug("Instantiating flapping-singleton-service.");
44         try {
45             registration.close();
46             registration = null;
47         } catch (Exception e) {
48             LOG.warn("There was a problem closing flapping singleton service.", e);
49             setInactive();
50
51             final long count = flapCount.get();
52             flapCount.compareAndSet(count, -count);
53         }
54     }
55
56     @Override
57     @SuppressWarnings("checkstyle:IllegalCatch")
58     public ListenableFuture<Void> closeServiceInstance() {
59         LOG.debug("Closing flapping-singleton-service, flapCount: {}", flapCount);
60
61         flapCount.incrementAndGet();
62         if (active.get()) {
63             LOG.debug("Running re-registration");
64             try {
65                 registration = singletonServiceProvider.registerClusterSingletonService(this);
66             } catch (RuntimeException e) {
67                 LOG.warn("There was a problem re-registering flapping singleton service.", e);
68                 setInactive();
69
70                 final long count = flapCount.get();
71                 flapCount.compareAndSet(count, -count - 1);
72             }
73         }
74
75         return Futures.immediateFuture(null);
76     }
77
78     @Override
79     public ServiceGroupIdentifier getIdentifier() {
80         return SERVICE_GROUP_IDENTIFIER;
81     }
82
83     public long setInactive() {
84         LOG.debug("Setting flapping-singleton-service to inactive, flap-count: {}", flapCount);
85
86         active.set(false);
87         return flapCount.get();
88     }
89 }