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