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