2 * Copyright (c) 2016, 2017 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
8 package org.opendaylight.openflowplugin.applications.frm.impl;
10 import com.google.common.collect.ImmutableSet;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.concurrent.atomic.AtomicBoolean;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.mdsal.binding.api.RpcProviderService;
16 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
17 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.frm.reconciliation.service.rev180227.FrmReconciliationService;
24 import org.opendaylight.yangtools.concepts.ObjectRegistration;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Service (per device) for registration in singleton provider.
33 public class DeviceMastership implements ClusterSingletonService, AutoCloseable {
34 private static final Logger LOG = LoggerFactory.getLogger(DeviceMastership.class);
35 private final NodeId nodeId;
36 private final ServiceGroupIdentifier identifier;
37 private final AtomicBoolean deviceMastered = new AtomicBoolean(false);
38 private final AtomicBoolean isDeviceInOperDS = new AtomicBoolean(false);
39 private final InstanceIdentifier<FlowCapableNode> fcnIID;
40 private final KeyedInstanceIdentifier<Node, NodeKey> path;
42 private ObjectRegistration<@NonNull FrmReconciliationService> reg;
44 public DeviceMastership(final NodeId nodeId) {
46 this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
47 fcnIID = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId))
48 .augmentation(FlowCapableNode.class);
49 path = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
53 public void instantiateServiceInstance() {
54 LOG.info("FRM started for: {}", nodeId.getValue());
55 deviceMastered.set(true);
59 public ListenableFuture<Void> closeServiceInstance() {
60 LOG.info("FRM stopped for: {}", nodeId.getValue());
61 deviceMastered.set(false);
62 return Futures.immediateFuture(null);
66 public ServiceGroupIdentifier getIdentifier() {
74 public boolean isDeviceMastered() {
75 return deviceMastered.get();
78 public void setDeviceOperationalStatus(final boolean inOperDS) {
79 isDeviceInOperDS.set(inOperDS);
82 public void reconcile() {
83 deviceMastered.set(true);
86 public void registerReconciliationRpc(final RpcProviderService rpcProviderService,
87 final FrmReconciliationService reconcliationService) {
89 LOG.debug("The path is registered : {}", path);
90 reg = rpcProviderService.registerRpcImplementation(FrmReconciliationService.class, reconcliationService,
91 ImmutableSet.of(path));
93 LOG.debug("The path is already registered : {}", path);
97 public void deregisterReconciliationRpc() {
101 LOG.debug("The path is unregistered : {}", path);
103 LOG.debug("The path is already unregistered : {}", path);