Bump MRI upstreams
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / clustering / DeviceMastership.java
1 /*
2  * Copyright (c) 2016 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.openflowplugin.applications.frsync.impl.clustering;
9
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
13 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
14 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
15 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
16 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * {@link ClusterSingletonService} clusterSingletonServiceRegistration per connected device.
23  */
24 public final class DeviceMastership implements ClusterSingletonService, AutoCloseable {
25     private static final Logger LOG = LoggerFactory.getLogger(DeviceMastership.class);
26     private final NodeId nodeId;
27     private final ServiceGroupIdentifier identifier;
28     private final ReconciliationRegistry reconciliationRegistry;
29     private final ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
30     private boolean deviceMastered;
31
32     public DeviceMastership(final NodeId nodeId,
33                             final ReconciliationRegistry reconciliationRegistry,
34                             final ClusterSingletonServiceProvider clusterSingletonService) {
35         this.nodeId = nodeId;
36         identifier = ServiceGroupIdentifier.create(nodeId.getValue());
37         this.reconciliationRegistry = reconciliationRegistry;
38         deviceMastered = false;
39         clusterSingletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
40     }
41
42     @Override
43     public void instantiateServiceInstance() {
44         LOG.debug("FRS started for: {}", nodeId.getValue());
45         deviceMastered = true;
46         reconciliationRegistry.register(nodeId);
47     }
48
49     @Override
50     public ListenableFuture<Void> closeServiceInstance() {
51         LOG.debug("FRS stopped for: {}", nodeId.getValue());
52         deviceMastered = false;
53         reconciliationRegistry.unregisterIfRegistered(nodeId);
54         return Futures.immediateFuture(null);
55     }
56
57     @Override
58     public ServiceGroupIdentifier getIdentifier() {
59         return identifier;
60     }
61
62     @Override
63     @SuppressWarnings("checkstyle:IllegalCatch")
64     public void close() {
65         if (clusterSingletonServiceRegistration != null) {
66             try {
67                 clusterSingletonServiceRegistration.close();
68             } catch (Exception e) {
69                 LOG.error("FRS cluster service close fail: {}", nodeId.getValue(), e);
70             }
71         }
72     }
73
74     public boolean isDeviceMastered() {
75         return deviceMastered;
76     }
77
78 }