Merge "Removed duplicate declaration in pom.xml."
[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.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 class DeviceMastership implements ClusterSingletonService {
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 ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
30     private boolean deviceMastered;
31
32     public DeviceMastership(final NodeId nodeId, final ReconciliationRegistry reconciliationRegistry) {
33         this.nodeId = nodeId;
34         this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
35         this.reconciliationRegistry = reconciliationRegistry;
36         this.deviceMastered = false;
37     }
38
39     @Override
40     public void instantiateServiceInstance() {
41         LOG.debug("FRS started for: {}", nodeId.getValue());
42         deviceMastered = true;
43         reconciliationRegistry.register(nodeId);
44     }
45
46     @Override
47     public ListenableFuture<Void> closeServiceInstance() {
48         LOG.debug("FRS stopped for: {}", nodeId.getValue());
49         deviceMastered = false;
50         reconciliationRegistry.unregisterIfRegistered(nodeId);
51         return Futures.immediateFuture(null);
52     }
53
54     @Override
55     public ServiceGroupIdentifier getIdentifier() {
56         return identifier;
57     }
58
59     public boolean isDeviceMastered() {
60         return deviceMastered;
61     }
62
63     public void setClusterSingletonServiceRegistration(final ClusterSingletonServiceRegistration registration) {
64         this.clusterSingletonServiceRegistration = registration;
65     }
66
67     public ClusterSingletonServiceRegistration getClusterSingletonServiceRegistration() {
68         return clusterSingletonServiceRegistration;
69     }
70
71 }