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;
14 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
15 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Manager for clustering service registrations of {@link DeviceMastership}.
30 public class DeviceMastershipManager implements OpendaylightInventoryListener, AutoCloseable{
31 private static final Logger LOG = LoggerFactory.getLogger(DeviceMastershipManager.class);
32 private final ClusterSingletonServiceProvider clusterSingletonService;
33 private final ListenerRegistration<?> notifListenerRegistration;
34 private final ConcurrentHashMap<NodeId, DeviceMastership> deviceMasterships = new ConcurrentHashMap();
36 public DeviceMastershipManager(final ClusterSingletonServiceProvider clusterSingletonService,
37 final NotificationProviderService notificationService) {
38 this.clusterSingletonService = clusterSingletonService;
39 this.notifListenerRegistration = notificationService.registerNotificationListener(this);
42 public void onDeviceConnected(final NodeId nodeId) {
46 public void onDeviceDisconnected(final NodeId nodeId) {
50 public boolean isDeviceMastered(final NodeId nodeId) {
51 return deviceMasterships.get(nodeId) != null && deviceMasterships.get(nodeId).isDeviceMastered();
55 ConcurrentHashMap<NodeId, DeviceMastership> getDeviceMasterships() {
56 return deviceMasterships;
60 public void onNodeUpdated(NodeUpdated notification) {
61 LOG.debug("NodeUpdate notification received : {}", notification);
62 DeviceMastership membership = deviceMasterships.computeIfAbsent(notification.getId(), device ->
63 new DeviceMastership(notification.getId(), clusterSingletonService));
64 membership.registerClusterSingletonService();
68 public void onNodeConnectorUpdated(NodeConnectorUpdated notification) {
69 //Not published by plugin
73 public void onNodeRemoved(NodeRemoved notification) {
74 LOG.debug("NodeRemoved notification received : {}", notification);
75 NodeId nodeId = notification.getNodeRef().getValue().firstKeyOf(Node.class).getId();
76 final DeviceMastership mastership = deviceMasterships.remove(nodeId);
77 if (mastership != null) {
79 LOG.info("Unregistered FRM cluster singleton service for service id : {}", nodeId.getValue());
84 public void onNodeConnectorRemoved(NodeConnectorRemoved notification) {
85 //Not published by plugin
89 public void close() throws Exception {
90 if (notifListenerRegistration != null) {
91 notifListenerRegistration.close();