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.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.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Service (per device) for registration in singleton provider.
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 ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
29 private boolean deviceMastered;
31 public DeviceMastership(final NodeId nodeId, final ClusterSingletonServiceProvider clusterSingletonService) {
33 this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
34 this.deviceMastered = false;
35 clusterSingletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
39 public void instantiateServiceInstance() {
40 LOG.debug("FRM started for: {}", nodeId.getValue());
41 deviceMastered = true;
45 public ListenableFuture<Void> closeServiceInstance() {
46 LOG.debug("FRM stopped for: {}", nodeId.getValue());
47 deviceMastered = false;
48 return Futures.immediateFuture(null);
52 public ServiceGroupIdentifier getIdentifier() {
58 if (clusterSingletonServiceRegistration != null) {
60 clusterSingletonServiceRegistration.close();
61 } catch (Exception e) {
62 LOG.error("FRM cluster service close fail: {} {}", nodeId.getValue(), e);
67 public boolean isDeviceMastered() {
68 return deviceMastered;