NEUTRON-208: Neutronvpn changes for BGPVPN network and router association
[netvirt.git] / neutronvpn / impl / src / main / java / org / opendaylight / netvirt / neutronvpn / NeutronBgpvpnUtils.java
1 /*
2  * Copyright (c) 2020 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.neutronvpn;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.ConcurrentHashMap;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.infrautils.utils.concurrent.NamedLocks;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 @Singleton
22 public class NeutronBgpvpnUtils {
23
24     private static final Logger LOG = LoggerFactory.getLogger(NeutronBgpvpnUtils.class);
25
26     private final ConcurrentHashMap<Uuid, List<Uuid>> unprocessedNetworksMap;
27     private final ConcurrentHashMap<Uuid, List<Uuid>> unprocessedRoutersMap;
28     private final NamedLocks<String> vpnLock;
29
30     @Inject
31     public NeutronBgpvpnUtils() {
32         unprocessedNetworksMap = new ConcurrentHashMap<>();
33         unprocessedRoutersMap = new ConcurrentHashMap<>();
34         vpnLock = new NamedLocks<>();
35     }
36
37     public void addUnProcessedNetwork(Uuid vpnId, Uuid networkId) {
38         LOG.trace("Adding Unprocessed Network to Bgpvpn : bgpvpnId: {}, networkId={}", vpnId.getValue(),
39                 networkId.getValue());
40         List<Uuid> unProcessedNetworkIds = unprocessedNetworksMap.get(vpnId);
41         if (unProcessedNetworkIds == null) {
42             unProcessedNetworkIds = new ArrayList<>();
43             unProcessedNetworkIds.add(networkId);
44             unprocessedNetworksMap.putIfAbsent(vpnId, unProcessedNetworkIds);
45         } else {
46             if (!unProcessedNetworkIds.contains(networkId)) {
47                 unProcessedNetworkIds.add(networkId);
48             }
49         }
50     }
51
52     public void removeUnProcessedNetwork(Uuid vpnId, Uuid networkId) {
53         LOG.trace("Removing Unprocessed Network to Bgpvpn : bgpvpnId: {}, networkId={}", vpnId.getValue(),
54                 networkId.getValue());
55         List<Uuid> unProcessedNetworkIds = unprocessedNetworksMap.get(vpnId);
56         if (unProcessedNetworkIds != null) {
57             unProcessedNetworkIds.remove(networkId);
58         }
59     }
60
61     public void addUnProcessedRouter(Uuid vpnId, Uuid routerId) {
62         LOG.trace("Adding Unprocessed Router to Bgpvpn : bgpvpnId: {}, routerId={}", vpnId.getValue(),
63                 routerId.getValue());
64         List<Uuid> unProcessedRouterIds = unprocessedRoutersMap.get(vpnId);
65         if (unProcessedRouterIds == null) {
66             unProcessedRouterIds =  new ArrayList<>();
67             unProcessedRouterIds.add(routerId);
68             unprocessedRoutersMap.putIfAbsent(vpnId, unProcessedRouterIds);
69         } else {
70             if (!unProcessedRouterIds.contains(routerId)) {
71                 unProcessedRouterIds.add(routerId);
72             }
73         }
74     }
75
76     public void removeUnProcessedRouter(Uuid vpnId, Uuid routerId) {
77         LOG.trace("Removing Unprocessed Router to Bgpvpn : bgpvpnId: {}, routerId={}", vpnId.getValue(),
78                 routerId.getValue());
79         List<Uuid> unProcessedRouterIds = unprocessedRoutersMap.get(vpnId);
80         if (unProcessedRouterIds != null) {
81             unProcessedRouterIds.remove(routerId);
82         }
83     }
84
85     public NamedLocks<String> getVpnLock() {
86         return vpnLock;
87     }
88
89     public ConcurrentHashMap<Uuid, List<Uuid>> getUnProcessedRoutersMap() {
90         return unprocessedRoutersMap;
91     }
92
93     public ConcurrentHashMap<Uuid, List<Uuid>> getUnProcessedNetworksMap() {
94         return unprocessedNetworksMap;
95     }
96
97     public List<Uuid> getUnprocessedNetworksForBgpvpn(Uuid vpnId) {
98         return unprocessedNetworksMap.get(vpnId);
99     }
100
101     public List<Uuid> getUnprocessedRoutersForBgpvpn(Uuid vpnId) {
102         return unprocessedRoutersMap.get(vpnId);
103     }
104 }