2 * Copyright (c) 2016 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.applications.frm.impl;
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.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
19 * Manager for clustering service registrations of {@link DeviceMastership}.
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();
26 public DeviceMastershipManager(final ClusterSingletonServiceProvider clusterSingletonService) {
27 this.clusterSingletonService = clusterSingletonService;
30 public void onDeviceConnected(final NodeId nodeId) {
31 LOG.debug("FRM service registered for: {}", nodeId.getValue());
32 final DeviceMastership mastership = new DeviceMastership(nodeId, clusterSingletonService);
33 deviceMasterships.put(nodeId, mastership);
36 public void onDeviceDisconnected(final NodeId nodeId) {
37 final DeviceMastership mastership = deviceMasterships.remove(nodeId);
38 if (mastership != null) {
41 LOG.debug("FRM service unregistered for: {}", nodeId.getValue());
44 public boolean isDeviceMastered(final NodeId nodeId) {
45 return deviceMasterships.get(nodeId) != null && deviceMasterships.get(nodeId).isDeviceMastered();
49 ConcurrentHashMap<NodeId, DeviceMastership> getDeviceMasterships() {
50 return deviceMasterships;