Merge "Bug 6374 topology-manager - DTCL instead of DTL"
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / DeviceMastershipManager.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 java.util.concurrent.ConcurrentHashMap;
12 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
13 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * Manager for clustering service registrations of {@link DeviceMastership}.
20  */
21 public class DeviceMastershipManager {
22     private static final Logger LOG = LoggerFactory.getLogger(DeviceMastershipManager.class);
23     private final ClusterSingletonServiceProvider clusterSingletonService;
24     private final ConcurrentHashMap<NodeId, DeviceMastership> deviceMasterships = new ConcurrentHashMap();
25
26     public DeviceMastershipManager(final ClusterSingletonServiceProvider clusterSingletonService) {
27         this.clusterSingletonService = clusterSingletonService;
28     }
29
30     public void onDeviceConnected(final NodeId nodeId) {
31         final DeviceMastership mastership = new DeviceMastership(nodeId);
32         final ClusterSingletonServiceRegistration registration = clusterSingletonService.registerClusterSingletonService(mastership);
33         mastership.setClusterSingletonServiceRegistration(registration);
34         deviceMasterships.put(nodeId, mastership);
35         LOG.debug("FRS service registered for: {}", nodeId.getValue());
36     }
37
38
39     public void onDeviceDisconnected(final NodeId nodeId) {
40         final DeviceMastership mastership = deviceMasterships.remove(nodeId);
41         final ClusterSingletonServiceRegistration registration = mastership.getClusterSingletonServiceRegistration();
42         if (registration != null) {
43             try {
44                 registration.close();
45             } catch (Exception e) {
46                 LOG.error("FRS cluster service close fail: {} {}", nodeId.getValue(), e);
47             }
48         }
49         LOG.debug("FRS service unregistered for: {}", nodeId.getValue());
50     }
51
52     public boolean isDeviceMastered(final NodeId nodeId) {
53         return deviceMasterships.get(nodeId) != null && deviceMasterships.get(nodeId).isDeviceMastered();
54     }
55
56 }