Merge "Bug 6374 topology-manager - DTCL instead of DTL"
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / DeviceMastership.java
1 /**
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. 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.frm.impl;
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.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Service (per device) for registration in singleton provider.
22  */
23 public class DeviceMastership implements ClusterSingletonService {
24     private static final Logger LOG = LoggerFactory.getLogger(DeviceMastership.class);
25     private final NodeId nodeId;
26     private final ServiceGroupIdentifier identifier;
27     private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
28     private boolean deviceMastered;
29
30     public DeviceMastership(final NodeId nodeId) {
31         this.nodeId = nodeId;
32         this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
33         this.deviceMastered = false;
34     }
35
36     @Override
37     public void instantiateServiceInstance() {
38         LOG.debug("FRM started for: {}", nodeId.getValue());
39         deviceMastered = true;
40     }
41
42     @Override
43     public ListenableFuture<Void> closeServiceInstance() {
44         LOG.debug("FRM stopped for: {}", nodeId.getValue());
45         deviceMastered = false;
46         return Futures.immediateFuture(null);
47     }
48
49     @Override
50     public ServiceGroupIdentifier getIdentifier() {
51         return identifier;
52     }
53
54     public boolean isDeviceMastered() {
55         return deviceMastered;
56     }
57
58     public void setClusterSingletonServiceRegistration(final ClusterSingletonServiceRegistration registration) {
59         this.clusterSingletonServiceRegistration = registration;
60     }
61
62     public ClusterSingletonServiceRegistration getClusterSingletonServiceRegistration() {
63         return clusterSingletonServiceRegistration;
64     }
65
66 }