Bulk merge of l2gw changes
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / HwvtepConfigNodeCache.java
1 /*
2  * Copyright (c) 2019 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
9 package org.opendaylight.netvirt.elan.l2gw.listeners;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.concurrent.ConcurrentHashMap;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.genius.mdsalutil.cache.InstanceIdDataObjectCache;
18 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
19 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
20 import org.opendaylight.infrautils.caches.CacheProvider;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28
29 @Singleton
30 public class HwvtepConfigNodeCache extends InstanceIdDataObjectCache<Node> {
31
32     private final DataBroker dataBroker;
33     private final Map<InstanceIdentifier<Node>, Node> cache = new ConcurrentHashMap<>();
34     private final Map<InstanceIdentifier<Node>, List<Runnable>> waitList = new ConcurrentHashMap<>();
35
36     @Inject
37     public HwvtepConfigNodeCache(final DataBroker dataBroker, CacheProvider cacheProvider) {
38         super(Node.class, dataBroker, LogicalDatastoreType.CONFIGURATION,
39             InstanceIdentifier.create(NetworkTopology.class)
40                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
41                 .child(Node.class), cacheProvider);
42         this.dataBroker = dataBroker;
43         ResourceBatchingManager.getInstance().registerDefaultBatchHandlers(this.dataBroker);
44     }
45
46     @Override
47     protected void removed(InstanceIdentifier<Node> key, Node deleted) {
48         cache.remove(key);
49     }
50
51     /*@Override
52     protected void update(InstanceIdentifier<Node> key, Node old, Node added) {
53         cache.put(key, added);
54     }*/
55
56     @Override
57     protected synchronized void added(InstanceIdentifier<Node> key, Node added) {
58         cache.put(key, added);
59         if (waitList.containsKey(key)) {
60             waitList.remove(key).stream().forEach(runnable -> runnable.run());
61         }
62     }
63
64     public Node getConfigNode(InstanceIdentifier<Node> key) {
65         return cache.get(key);
66     }
67
68     public synchronized void runAfterNodeAvailable(InstanceIdentifier<Node> key, Runnable runnable) {
69         if (cache.containsKey(key)) {
70             runnable.run();
71         } else {
72             waitList.putIfAbsent(key, new ArrayList<>());
73             waitList.get(key).add(runnable);
74         }
75     }
76 }