Merge "Bug 6146 - Some Flows not found in DeviceFlowRegistry"
[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.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
15 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
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  * Manager for clustering service registrations of {@link DeviceMastership}.
22  */
23 public class DeviceMastershipManager {
24     private static final Logger LOG = LoggerFactory.getLogger(DeviceMastershipManager.class);
25     private final ClusterSingletonServiceProvider clusterSingletonService;
26     private final ConcurrentHashMap<NodeId, DeviceMastership> deviceMasterships = new ConcurrentHashMap();
27     private final ReconciliationRegistry reconciliationRegistry;
28
29     public DeviceMastershipManager(final ClusterSingletonServiceProvider clusterSingletonService,
30                                    final ReconciliationRegistry reconciliationRegistry) {
31         this.clusterSingletonService = clusterSingletonService;
32         this.reconciliationRegistry = reconciliationRegistry;
33     }
34
35     public void onDeviceConnected(final NodeId nodeId) {
36         final DeviceMastership mastership = new DeviceMastership(nodeId, reconciliationRegistry);
37         final ClusterSingletonServiceRegistration registration = clusterSingletonService.registerClusterSingletonService(mastership);
38         mastership.setClusterSingletonServiceRegistration(registration);
39         deviceMasterships.put(nodeId, mastership);
40         LOG.debug("FRS service registered for: {}", nodeId.getValue());
41     }
42
43
44     public void onDeviceDisconnected(final NodeId nodeId) {
45         final DeviceMastership mastership = deviceMasterships.remove(nodeId);
46         final ClusterSingletonServiceRegistration registration = mastership.getClusterSingletonServiceRegistration();
47         if (registration != null) {
48             try {
49                 registration.close();
50             } catch (Exception e) {
51                 LOG.error("FRS cluster service close fail: {}", nodeId.getValue());
52             }
53         }
54         LOG.debug("FRS service unregistered for: {}", nodeId.getValue());
55     }
56
57     public boolean isDeviceMastered(final NodeId nodeId) {
58         return deviceMasterships.get(nodeId) != null && deviceMasterships.get(nodeId).isDeviceMastered();
59     }
60
61     @VisibleForTesting
62     ConcurrentHashMap<NodeId, DeviceMastership> getDeviceMasterships() {
63         return deviceMasterships;
64     }
65 }