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 ClusterSingletonServiceProvider clusterSingletonServiceProvider;
29 private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
30 private boolean deviceMastered;
32 public DeviceMastership(final NodeId nodeId, final ClusterSingletonServiceProvider clusterSingletonService) {
34 this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
35 this.deviceMastered = false;
36 this.clusterSingletonServiceProvider = clusterSingletonService;
40 public void instantiateServiceInstance() {
41 LOG.info("FRM started for: {}", nodeId.getValue());
42 deviceMastered = true;
46 public ListenableFuture<Void> closeServiceInstance() {
47 LOG.info("FRM stopped for: {}", nodeId.getValue());
48 deviceMastered = false;
49 return Futures.immediateFuture(null);
53 public ServiceGroupIdentifier getIdentifier() {
59 if (clusterSingletonServiceRegistration != null) {
61 clusterSingletonServiceRegistration.close();
62 } catch (Exception e) {
63 LOG.error("FRM cluster service close fail: {} {}", nodeId.getValue(), e);
68 public boolean isDeviceMastered() {
69 return deviceMastered;
72 public void registerClusterSingletonService() {
73 LOG.info("Registering FRM as a cluster singleton service listner for service id : {}",getIdentifier());
74 clusterSingletonServiceRegistration = clusterSingletonServiceProvider.registerClusterSingletonService(this);