71f39e424e11cf3aebf0885401467e3f0a09c51a
[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.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.concurrent.atomic.AtomicBoolean;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
15 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
16 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
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.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Service (per device) for registration in singleton provider.
30  */
31 public class DeviceMastership implements ClusterSingletonService, AutoCloseable {
32     private static final Logger LOG = LoggerFactory.getLogger(DeviceMastership.class);
33     private final NodeId nodeId;
34     private final ServiceGroupIdentifier identifier;
35     private final AtomicBoolean deviceMastered = new AtomicBoolean(false);
36     private final AtomicBoolean isDeviceInOperDS = new AtomicBoolean(false);
37     private final InstanceIdentifier<FlowCapableNode> fcnIID;
38     private final KeyedInstanceIdentifier<Node, NodeKey> path;
39     private final RoutedRpcRegistration routedRpcReg;
40
41     public DeviceMastership(final NodeId nodeId, final RoutedRpcRegistration routedRpcReg) {
42         this.nodeId = nodeId;
43         this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
44         fcnIID = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId))
45                 .augmentation(FlowCapableNode.class);
46         path = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
47         this.routedRpcReg = routedRpcReg;
48     }
49
50     @Override
51     public void instantiateServiceInstance() {
52         LOG.info("FRM started for: {}", nodeId.getValue());
53         deviceMastered.set(true);
54     }
55
56     @Override
57     public ListenableFuture<Void> closeServiceInstance() {
58         LOG.info("FRM stopped for: {}", nodeId.getValue());
59         deviceMastered.set(false);
60         return Futures.immediateFuture(null);
61     }
62
63     @Override
64     public ServiceGroupIdentifier getIdentifier() {
65         return identifier;
66     }
67
68     @Override
69     public void close() {
70     }
71
72     public boolean isDeviceMastered() {
73         return deviceMastered.get();
74     }
75
76     public void setDeviceOperationalStatus(boolean inOperDS) {
77         isDeviceInOperDS.set(inOperDS);
78     }
79
80     public void reconcile() {
81         deviceMastered.set(true);
82     }
83
84     public void registerReconciliationRpc() {
85         LOG.debug("The path is registered : {}", path);
86         routedRpcReg.registerPath(NodeContext.class, path);
87     }
88
89     public void deregisterReconciliationRpc() {
90         LOG.debug("The path is unregistered : {}", path);
91         routedRpcReg.unregisterPath(NodeContext.class, path);
92     }
93 }