BUG-7976: Race between peer removal and routes update
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / ExportPolicyPeerTrackerImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.protocol.bgp.rib.impl;
9
10 import com.google.common.base.Preconditions;
11 import java.util.EnumMap;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Map;
15 import java.util.Optional;
16 import java.util.Set;
17 import javax.annotation.concurrent.GuardedBy;
18 import javax.annotation.concurrent.ThreadSafe;
19 import org.opendaylight.protocol.bgp.rib.impl.spi.PeerExportGroupRegistry;
20 import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker;
21 import org.opendaylight.protocol.bgp.rib.spi.PeerExportGroup;
22 import org.opendaylight.protocol.bgp.rib.spi.PeerExportGroup.PeerExporTuple;
23 import org.opendaylight.protocol.concepts.AbstractRegistration;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.SendReceive;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.SimpleRoutingPolicy;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 @ThreadSafe
34 final class ExportPolicyPeerTrackerImpl implements ExportPolicyPeerTracker {
35     private static final Logger LOG = LoggerFactory.getLogger(ExportPolicyPeerTrackerImpl.class);
36     @GuardedBy("this")
37     private final Map<YangInstanceIdentifier, PeerRole> peerRoles = new HashMap<>();
38     @GuardedBy("this")
39     private final Map<PeerId, SendReceive> peerAddPathTables = new HashMap<>();
40     @GuardedBy("this")
41     private final Set<PeerId> peerTables = new HashSet<>();
42     private final PolicyDatabase policyDatabase;
43     private final TablesKey localTableKey;
44     @GuardedBy("this")
45     private final Map<PeerRole, PeerExportGroupRegistry> groups = new EnumMap<>(PeerRole.class);
46
47     ExportPolicyPeerTrackerImpl(final PolicyDatabase policyDatabase, final TablesKey localTablesKey) {
48         this.policyDatabase = Preconditions.checkNotNull(policyDatabase);
49         this.localTableKey = localTablesKey;
50     }
51
52     private synchronized AbstractRegistration addToExportGroups(final PeerId peerId,
53         final YangInstanceIdentifier peerPath, final PeerRole peerRole) {
54         final PeerExportGroupRegistry peerExp = this.groups.computeIfAbsent(peerRole,
55             k -> new PeerExportGroupImpl(this.policyDatabase.exportPolicyForRole(peerRole)));
56
57         final AbstractRegistration registration =  peerExp.registerPeer(peerId, new PeerExporTuple(peerPath, peerRole));
58
59         return new AbstractRegistration() {
60             @Override
61             protected void removeRegistration() {
62                 registration.close();
63                 if(ExportPolicyPeerTrackerImpl.this.groups.get(peerRole).isEmpty()) {
64                     ExportPolicyPeerTrackerImpl.this.groups.remove(peerRole);
65                 }
66             }
67         };
68     }
69
70     @Override
71     public synchronized AbstractRegistration registerPeer(final PeerId peerId, final SendReceive sendReceive,
72         final YangInstanceIdentifier peerPath, final PeerRole peerRole,
73         final Optional<SimpleRoutingPolicy> optSimpleRoutingPolicy) {
74         if (sendReceive != null) {
75             this.peerAddPathTables.put(peerId, sendReceive);
76             LOG.debug("Supported Add BestPath table {} added to peer {}", sendReceive, peerId);
77         }
78         final SimpleRoutingPolicy simpleRoutingPolicy = optSimpleRoutingPolicy.orElse(null);
79         if (SimpleRoutingPolicy.AnnounceNone != simpleRoutingPolicy) {
80             this.peerTables.add(peerId);
81         }
82         this.peerRoles.put(peerPath, peerRole);
83         LOG.debug("Supported table {} added to peer {} role {}", this.localTableKey, peerId, peerRole);
84         final AbstractRegistration registration = addToExportGroups(peerId, peerPath, peerRole);
85
86         final Object lock = this;
87         return new AbstractRegistration() {
88             @Override
89             protected void removeRegistration() {
90                 synchronized (lock) {
91                     final SendReceive sendReceiveValue = ExportPolicyPeerTrackerImpl.this.peerAddPathTables.remove(peerId);
92                     if (sendReceiveValue != null) {
93                         LOG.debug("Supported Add BestPath table {} removed to peer {}", sendReceiveValue, peerId);
94                     }
95                     ExportPolicyPeerTrackerImpl.this.peerTables.remove(peerId);
96                     LOG.debug("Removed peer {} from supported table {}", peerId, ExportPolicyPeerTrackerImpl.this.localTableKey);
97                     ExportPolicyPeerTrackerImpl.this.peerRoles.remove(peerPath);
98                     registration.close();
99                 }
100             }
101         };
102     }
103
104     @Override
105     public synchronized PeerExportGroup getPeerGroup(final PeerRole role) {
106         return this.groups.get(Preconditions.checkNotNull(role));
107     }
108
109     @Override
110     public synchronized boolean isTableSupported(final PeerId peerId) {
111         return this.peerTables.contains(peerId);
112     }
113
114     @Override
115     public synchronized PeerRole getRole(final YangInstanceIdentifier peerId) {
116         return this.peerRoles.get(peerId);
117     }
118
119     @Override
120     public synchronized boolean isAddPathSupportedByPeer(final PeerId peerId) {
121         final SendReceive sendReceive = this.peerAddPathTables.get(peerId);
122         return sendReceive != null && (sendReceive.equals(SendReceive.Both) || sendReceive.equals(SendReceive.Receive));
123     }
124 }