Bug 6110: Fixed bugs in statistics manager due to race condition.
[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.ClusterSingletonServiceProvider;
15 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
16 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
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  * Service (per device) for registration in singleton provider.
23  */
24 public 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 ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
29     private boolean deviceMastered;
30
31     public DeviceMastership(final NodeId nodeId, final ClusterSingletonServiceProvider clusterSingletonService) {
32         this.nodeId = nodeId;
33         this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
34         this.deviceMastered = false;
35         clusterSingletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
36     }
37
38     @Override
39     public void instantiateServiceInstance() {
40         LOG.debug("FRM started for: {}", nodeId.getValue());
41         deviceMastered = true;
42     }
43
44     @Override
45     public ListenableFuture<Void> closeServiceInstance() {
46         LOG.debug("FRM stopped for: {}", nodeId.getValue());
47         deviceMastered = false;
48         return Futures.immediateFuture(null);
49     }
50
51     @Override
52     public ServiceGroupIdentifier getIdentifier() {
53         return identifier;
54     }
55
56     @Override
57     public void close() {
58         if (clusterSingletonServiceRegistration != null) {
59             try {
60                 clusterSingletonServiceRegistration.close();
61             } catch (Exception e) {
62                 LOG.error("FRM cluster service close fail: {} {}", nodeId.getValue(), e);
63             }
64         }
65     }
66
67     public boolean isDeviceMastered() {
68         return deviceMastered;
69     }
70
71 }