Merge "BUG-6118: making the OFentityListener aware of the InJeopardy() flag"
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / clustering / DeviceMastershipManager.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.annotations.VisibleForTesting;
12 import java.util.concurrent.ConcurrentHashMap;
13 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
14 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Manager for clustering service registrations of {@link DeviceMastership}.
21  */
22 public class DeviceMastershipManager {
23     private static final Logger LOG = LoggerFactory.getLogger(DeviceMastershipManager.class);
24     private final ClusterSingletonServiceProvider clusterSingletonService;
25     private final ConcurrentHashMap<NodeId, DeviceMastership> deviceMasterships = new ConcurrentHashMap();
26     private final ReconciliationRegistry reconciliationRegistry;
27
28     public DeviceMastershipManager(final ClusterSingletonServiceProvider clusterSingletonService,
29                                    final ReconciliationRegistry reconciliationRegistry) {
30         this.clusterSingletonService = clusterSingletonService;
31         this.reconciliationRegistry = reconciliationRegistry;
32     }
33
34     public void onDeviceConnected(final NodeId nodeId) {
35         final DeviceMastership mastership = new DeviceMastership(nodeId, reconciliationRegistry, clusterSingletonService);
36         deviceMasterships.put(nodeId, mastership);
37         LOG.debug("FRS service registered for: {}", nodeId.getValue());
38     }
39
40     public void onDeviceDisconnected(final NodeId nodeId) {
41         final DeviceMastership mastership = deviceMasterships.remove(nodeId);
42         if (mastership != null) {
43             mastership.close();
44         }
45         LOG.debug("FRS service unregistered for: {}", nodeId.getValue());
46     }
47
48     public boolean isDeviceMastered(final NodeId nodeId) {
49         return deviceMasterships.get(nodeId) != null && deviceMasterships.get(nodeId).isDeviceMastered();
50     }
51
52     @VisibleForTesting
53     ConcurrentHashMap<NodeId, DeviceMastership> getDeviceMasterships() {
54         return deviceMasterships;
55     }
56
57 }