Updated L2Gw changes in "neutronvpn", "elanmanager" and "dhcpservice" modules
[vpnservice.git] / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / vpnservice / elan / l2gw / listeners / HwvtepPhysicalLocatorListener.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.vpnservice.elan.l2gw.listeners;
9
10 import com.google.common.collect.Lists;
11 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.vpnservice.datastoreutils.AsyncClusteredDataChangeListenerBase;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
25
26 import java.util.List;
27 import java.util.Map;
28 import java.util.concurrent.ConcurrentHashMap;
29
30
31 /**
32  * Listener for physical locator presence in operational datastore
33  *
34  *
35  *
36  */
37 public class HwvtepPhysicalLocatorListener extends
38         AsyncClusteredDataChangeListenerBase<TerminationPoint, HwvtepPhysicalLocatorListener> implements AutoCloseable {
39
40     private DataBroker broker;
41     private ListenerRegistration<DataChangeListener> lstnerRegistration;
42
43     private static final Logger logger = LoggerFactory.getLogger(HwvtepPhysicalLocatorListener.class);
44
45     public HwvtepPhysicalLocatorListener(DataBroker broker) {
46         super(TerminationPoint.class, HwvtepPhysicalLocatorListener.class);
47
48         this.broker = broker;
49         registerListener();
50         logger.debug("created HwvtepPhysicalLocatorListener");
51     }
52
53     static Map<InstanceIdentifier<TerminationPoint>, List<Runnable>> waitingJobsList = new ConcurrentHashMap<>();
54     static Map<InstanceIdentifier<TerminationPoint>, Boolean> teps = new ConcurrentHashMap<>();
55
56     public static void runJobAfterPhysicalLocatorIsAvialable(InstanceIdentifier<TerminationPoint> key, Runnable runnable) {
57         if (teps.get(key) != null) {
58             logger.debug("physical locator already available {} running job ", key);
59             runnable.run();
60             return;
61         }
62         synchronized (HwvtepPhysicalLocatorListener.class) {
63             List<Runnable> list = waitingJobsList.get(key);
64             if (list == null) {
65                 waitingJobsList.put(key, Lists.newArrayList(runnable));
66             } else {
67                 list.add(runnable);
68             }
69             logger.debug("added the job to wait list of physical locator {}", key);
70         }
71     }
72
73     protected void registerListener() {
74         try {
75             lstnerRegistration = this.broker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
76                     InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class).
77                             child(TerminationPoint.class), this, DataChangeScope.BASE);
78         } catch (final Exception e) {
79             logger.error("Hwvtep LocalUcasMacs DataChange listener registration failed !", e);
80             throw new IllegalStateException("Hwvtep LocalUcasMacs DataChange listener registration failed .", e);
81         }
82     }
83
84     @Override
85     public void close() throws Exception {
86         if (lstnerRegistration != null) {
87             try {
88                 lstnerRegistration.close();
89             } catch (final Exception e) {
90                 logger.error("Error when cleaning up DataChangeListener.", e);
91             }
92             lstnerRegistration = null;
93         }
94     }
95
96     @Override
97     protected void remove(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint del) {
98         logger.trace("physical locator removed {}", identifier);
99         teps.remove(identifier);
100     }
101
102     @Override
103     protected void update(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint original, TerminationPoint update) {
104         logger.trace("physical locator available {}", identifier);
105     }
106
107     @Override
108     protected void add(InstanceIdentifier<TerminationPoint> identifier, TerminationPoint add) {
109         logger.trace("physical locator available {}", identifier);
110         teps.put(identifier, true);
111         List<Runnable> runnableList = null;
112         synchronized (HwvtepPhysicalLocatorListener.class) {
113             runnableList = waitingJobsList.get(identifier);
114             waitingJobsList.remove(identifier);
115         }
116         if (runnableList != null) {
117             logger.debug("physical locator available {} running jobs ", identifier);
118             for (Runnable r : runnableList) {
119                 r.run();
120             }
121         } else {
122             logger.debug("no jobs are waiting for physical locator {}", identifier);
123         }
124     }
125
126     @Override
127     protected InstanceIdentifier<TerminationPoint> getWildCardPath() {
128         return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class).child(Node.class).
129                 child(TerminationPoint.class);
130     }
131
132     @Override
133     protected ClusteredDataChangeListener getDataChangeListener() {
134         return HwvtepPhysicalLocatorListener.this;
135     }
136
137     @Override
138     protected DataChangeScope getDataChangeScope() {
139         return DataChangeScope.BASE;
140     }
141 }