Merge "OFPGC_ADD_OR_MOD support in openflowplugin"
[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.mdsal.singleton.common.api.ClusterSingletonService;
15 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Service (per device) for registration in singleton provider.
27  */
28 public class DeviceMastership implements ClusterSingletonService, AutoCloseable {
29     private static final Logger LOG = LoggerFactory.getLogger(DeviceMastership.class);
30     private final NodeId nodeId;
31     private final ServiceGroupIdentifier identifier;
32     private final AtomicBoolean deviceMastered = new AtomicBoolean(false);
33     private final AtomicBoolean isDeviceInOperDS = new AtomicBoolean(false);
34     private final InstanceIdentifier<FlowCapableNode> fcnIID;
35
36     public DeviceMastership(final NodeId nodeId) {
37         this.nodeId = nodeId;
38         this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
39         fcnIID = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId))
40                 .augmentation(FlowCapableNode.class);
41     }
42
43     @Override
44     public void instantiateServiceInstance() {
45         LOG.info("FRM started for: {}", nodeId.getValue());
46         deviceMastered.set(true);
47     }
48
49     @Override
50     public ListenableFuture<Void> closeServiceInstance() {
51         LOG.info("FRM stopped for: {}", nodeId.getValue());
52         deviceMastered.set(false);
53         return Futures.immediateFuture(null);
54     }
55
56     @Override
57     public ServiceGroupIdentifier getIdentifier() {
58         return identifier;
59     }
60
61     @Override
62     public void close() {
63     }
64
65     public boolean isDeviceMastered() {
66         return deviceMastered.get();
67     }
68
69     public void setDeviceOperationalStatus(boolean inOperDS) {
70         isDeviceInOperDS.set(inOperDS);
71     }
72
73     public void reconcile() {
74         deviceMastered.set(true);
75     }
76 }