Convert ListenerRegistrationHelper into a component
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / DeviceMastership.java
1 /*
2  * Copyright (c) 2016, 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.applications.frm.impl;
9
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;
29
30 /**
31  * Service (per device) for registration in singleton provider.
32  */
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;
41
42     private ObjectRegistration<@NonNull FrmReconciliationService> reg;
43
44     public DeviceMastership(final NodeId nodeId) {
45         this.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));
50     }
51
52     @Override
53     public void instantiateServiceInstance() {
54         LOG.info("FRM started for: {}", nodeId.getValue());
55         deviceMastered.set(true);
56     }
57
58     @Override
59     public ListenableFuture<Void> closeServiceInstance() {
60         LOG.info("FRM stopped for: {}", nodeId.getValue());
61         deviceMastered.set(false);
62         return Futures.immediateFuture(null);
63     }
64
65     @Override
66     public ServiceGroupIdentifier getIdentifier() {
67         return identifier;
68     }
69
70     @Override
71     public void close() {
72     }
73
74     public boolean isDeviceMastered() {
75         return deviceMastered.get();
76     }
77
78     public void setDeviceOperationalStatus(final boolean inOperDS) {
79         isDeviceInOperDS.set(inOperDS);
80     }
81
82     public void reconcile() {
83         deviceMastered.set(true);
84     }
85
86     public void registerReconciliationRpc(final RpcProviderService rpcProviderService,
87             final FrmReconciliationService reconcliationService) {
88         if (reg == null) {
89             LOG.debug("The path is registered : {}", path);
90             reg = rpcProviderService.registerRpcImplementation(FrmReconciliationService.class, reconcliationService,
91                 ImmutableSet.of(path));
92         } else {
93             LOG.debug("The path is already registered : {}", path);
94         }
95     }
96
97     public void deregisterReconciliationRpc() {
98         if (reg != null) {
99             reg.close();
100             reg = null;
101             LOG.debug("The path is unregistered : {}", path);
102         } else {
103             LOG.debug("The path is already unregistered : {}", path);
104         }
105     }
106 }