Merge "Bug 6110: Fixed bugs in statistics manager due to race condition." into stable...
[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 ClusterSingletonServiceProvider clusterSingletonServiceProvider;
29     private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
30     private boolean deviceMastered;
31
32     public DeviceMastership(final NodeId nodeId, final ClusterSingletonServiceProvider clusterSingletonService) {
33         this.nodeId = nodeId;
34         this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
35         this.deviceMastered = false;
36         this.clusterSingletonServiceProvider = clusterSingletonService;
37     }
38
39     @Override
40     public void instantiateServiceInstance() {
41         LOG.info("FRM started for: {}", nodeId.getValue());
42         deviceMastered = true;
43     }
44
45     @Override
46     public ListenableFuture<Void> closeServiceInstance() {
47         LOG.info("FRM stopped for: {}", nodeId.getValue());
48         deviceMastered = false;
49         return Futures.immediateFuture(null);
50     }
51
52     @Override
53     public ServiceGroupIdentifier getIdentifier() {
54         return identifier;
55     }
56
57     @Override
58     public void close() {
59         if (clusterSingletonServiceRegistration != null) {
60             try {
61                 clusterSingletonServiceRegistration.close();
62             } catch (Exception e) {
63                 LOG.error("FRM cluster service close fail: {} {}", nodeId.getValue(), e);
64             }
65         }
66     }
67
68     public boolean isDeviceMastered() {
69         return deviceMastered;
70     }
71
72     public void registerClusterSingletonService() {
73         LOG.info("Registering FRM as a cluster singleton service listner for service id : {}",getIdentifier());
74         clusterSingletonServiceRegistration = clusterSingletonServiceProvider.registerClusterSingletonService(this);
75     }
76 }