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