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