2 * Copyright (c) 2016 Cisco Systems, Inc. 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
8 package org.opendaylight.openflowplugin.applications.frsync.impl.clustering;
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
13 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
14 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
15 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
16 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * {@link ClusterSingletonService} clusterSingletonServiceRegistration per connected device.
24 public class DeviceMastership implements ClusterSingletonService, AutoCloseable {
25 private static final Logger LOG = LoggerFactory.getLogger(DeviceMastership.class);
26 private final NodeId nodeId;
27 private final ServiceGroupIdentifier identifier;
28 private final ReconciliationRegistry reconciliationRegistry;
29 private final ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
30 private boolean deviceMastered;
32 public DeviceMastership(final NodeId nodeId,
33 final ReconciliationRegistry reconciliationRegistry,
34 final ClusterSingletonServiceProvider clusterSingletonService) {
36 this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
37 this.reconciliationRegistry = reconciliationRegistry;
38 this.deviceMastered = false;
39 clusterSingletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
43 public void instantiateServiceInstance() {
44 LOG.debug("FRS started for: {}", nodeId.getValue());
45 deviceMastered = true;
46 reconciliationRegistry.register(nodeId);
50 public ListenableFuture<Void> closeServiceInstance() {
51 LOG.debug("FRS stopped for: {}", nodeId.getValue());
52 deviceMastered = false;
53 reconciliationRegistry.unregisterIfRegistered(nodeId);
54 return Futures.immediateFuture(null);
58 public ServiceGroupIdentifier getIdentifier() {
63 @SuppressWarnings("checkstyle:IllegalCatch")
65 if (clusterSingletonServiceRegistration != null) {
67 clusterSingletonServiceRegistration.close();
68 } catch (Exception e) {
69 LOG.error("FRS cluster service close fail: {}", nodeId.getValue(), e);
74 public boolean isDeviceMastered() {
75 return deviceMastered;