Switch to MD-SAL APIs
[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
9 package org.opendaylight.openflowplugin.applications.frm.impl;
10
11 import com.google.common.collect.ImmutableSet;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.concurrent.atomic.AtomicBoolean;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.binding.api.RpcProviderService;
17 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
18 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.frm.reconciliation.service.rev180227.FrmReconciliationService;
25 import org.opendaylight.yangtools.concepts.ObjectRegistration;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Service (per device) for registration in singleton provider.
33  */
34 public class DeviceMastership implements ClusterSingletonService, AutoCloseable {
35     private static final Logger LOG = LoggerFactory.getLogger(DeviceMastership.class);
36     private final NodeId nodeId;
37     private final ServiceGroupIdentifier identifier;
38     private final AtomicBoolean deviceMastered = new AtomicBoolean(false);
39     private final AtomicBoolean isDeviceInOperDS = new AtomicBoolean(false);
40     private final InstanceIdentifier<FlowCapableNode> fcnIID;
41     private final KeyedInstanceIdentifier<Node, NodeKey> path;
42
43     private ObjectRegistration<@NonNull FrmReconciliationService> reg;
44
45     public DeviceMastership(final NodeId nodeId) {
46         this.nodeId = nodeId;
47         this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
48         fcnIID = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId))
49                 .augmentation(FlowCapableNode.class);
50         path = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
51     }
52
53     @Override
54     public void instantiateServiceInstance() {
55         LOG.info("FRM started for: {}", nodeId.getValue());
56         deviceMastered.set(true);
57     }
58
59     @Override
60     public ListenableFuture<Void> closeServiceInstance() {
61         LOG.info("FRM stopped for: {}", nodeId.getValue());
62         deviceMastered.set(false);
63         return Futures.immediateFuture(null);
64     }
65
66     @Override
67     public ServiceGroupIdentifier getIdentifier() {
68         return identifier;
69     }
70
71     @Override
72     public void close() {
73     }
74
75     public boolean isDeviceMastered() {
76         return deviceMastered.get();
77     }
78
79     public void setDeviceOperationalStatus(final boolean inOperDS) {
80         isDeviceInOperDS.set(inOperDS);
81     }
82
83     public void reconcile() {
84         deviceMastered.set(true);
85     }
86
87     public void registerReconciliationRpc(final RpcProviderService rpcProviderService,
88             final FrmReconciliationService reconcliationService) {
89         if (reg == null) {
90             LOG.debug("The path is registered : {}", path);
91             reg = rpcProviderService.registerRpcImplementation(FrmReconciliationService.class, reconcliationService,
92                 ImmutableSet.of(path));
93         } else {
94             LOG.debug("The path is already registered : {}", path);
95         }
96     }
97
98     public void deregisterReconciliationRpc() {
99         if (reg != null) {
100             reg.close();
101             reg = null;
102             LOG.debug("The path is unregistered : {}", path);
103         } else {
104             LOG.debug("The path is already unregistered : {}", path);
105         }
106     }
107 }