2286338b07934acbd655416532676b33995e72f9
[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 com.google.common.annotations.VisibleForTesting;
12 import java.util.concurrent.ConcurrentHashMap;
13
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;
26
27 /**
28  * Manager for clustering service registrations of {@link DeviceMastership}.
29  */
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();
35
36     public DeviceMastershipManager(final ClusterSingletonServiceProvider clusterSingletonService,
37                                    final NotificationProviderService notificationService) {
38         this.clusterSingletonService = clusterSingletonService;
39         this.notifListenerRegistration = notificationService.registerNotificationListener(this);
40     }
41
42     public void onDeviceConnected(final NodeId nodeId) {
43         //No-op
44     }
45
46     public void onDeviceDisconnected(final NodeId nodeId) {
47         //No-op
48     }
49
50     public boolean isDeviceMastered(final NodeId nodeId) {
51         return deviceMasterships.get(nodeId) != null && deviceMasterships.get(nodeId).isDeviceMastered();
52     }
53
54     @VisibleForTesting
55     ConcurrentHashMap<NodeId, DeviceMastership> getDeviceMasterships() {
56         return deviceMasterships;
57     }
58
59     @Override
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();
65     }
66
67     @Override
68     public void onNodeConnectorUpdated(NodeConnectorUpdated notification) {
69         //Not published by plugin
70     }
71
72     @Override
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) {
78             mastership.close();
79             LOG.info("Unregistered FRM cluster singleton service for service id : {}", nodeId.getValue());
80         }
81     }
82
83     @Override
84     public void onNodeConnectorRemoved(NodeConnectorRemoved notification) {
85         //Not published by plugin
86     }
87
88     @Override
89     public void close() throws Exception {
90         if (notifListenerRegistration != null) {
91             notifListenerRegistration.close();
92         }
93     }
94 }