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
9 package org.opendaylight.openflowplugin.applications.frsync.impl.clustering;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
14 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
15 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
16 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
17 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * {@link ClusterSingletonService} clusterSingletonServiceRegistration per connected device.
25 public class DeviceMastership implements ClusterSingletonService, AutoCloseable {
26 private static final Logger LOG = LoggerFactory.getLogger(DeviceMastership.class);
27 private final NodeId nodeId;
28 private final ServiceGroupIdentifier identifier;
29 private final ReconciliationRegistry reconciliationRegistry;
30 private final ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
31 private boolean deviceMastered;
33 public DeviceMastership(final NodeId nodeId,
34 final ReconciliationRegistry reconciliationRegistry,
35 final ClusterSingletonServiceProvider clusterSingletonService) {
37 this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
38 this.reconciliationRegistry = reconciliationRegistry;
39 this.deviceMastered = false;
40 clusterSingletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
44 public void instantiateServiceInstance() {
45 LOG.debug("FRS started for: {}", nodeId.getValue());
46 deviceMastered = true;
47 reconciliationRegistry.register(nodeId);
51 public ListenableFuture<Void> closeServiceInstance() {
52 LOG.debug("FRS stopped for: {}", nodeId.getValue());
53 deviceMastered = false;
54 reconciliationRegistry.unregisterIfRegistered(nodeId);
55 return Futures.immediateFuture(null);
59 public ServiceGroupIdentifier getIdentifier() {
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;