blob: edbe75c16472db3957a5914580c4330273ab4dfb [file] [log] [blame]
Shashikanth VH3fe37982015-11-30 11:50:07 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Shashikanth VH3fe37982015-11-30 11:50:07 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5 * the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 * specific language governing permissions and limitations under the License.
12 */
13
14package org.onosproject.bgp.controller.impl;
15
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +000016import com.google.common.collect.Maps;
Jonathan Hart51539b82015-10-29 09:53:04 -070017import com.google.common.base.MoreObjects;
Shashikanth VH3fe37982015-11-30 11:50:07 +053018import org.onosproject.bgp.controller.BgpController;
19import org.onosproject.bgp.controller.BgpId;
Priyanka Bfc51c952016-03-26 14:30:33 +053020import org.onosproject.bgp.controller.BgpLinkListener;
Shashikanth VH3fe37982015-11-30 11:50:07 +053021import org.onosproject.bgp.controller.BgpLocalRib;
22import org.onosproject.bgp.controller.BgpNodeListener;
Maciej Skala3251a302019-03-26 09:16:55 +010023import org.onosproject.bgp.controller.BgpPrefixListener;
Shashikanth VH3fe37982015-11-30 11:50:07 +053024import org.onosproject.bgp.controller.BgpSessionInfo;
Priyanka Bfc51c952016-03-26 14:30:33 +053025import org.onosproject.bgpio.exceptions.BgpParseException;
Shashikanth VH3fe37982015-11-30 11:50:07 +053026import org.onosproject.bgpio.protocol.BgpLSNlri;
27import org.onosproject.bgpio.protocol.linkstate.BgpLinkLSIdentifier;
28import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
29import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier;
30import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
31import org.onosproject.bgpio.protocol.linkstate.BgpPrefixIPv4LSNlriVer4;
32import org.onosproject.bgpio.protocol.linkstate.BgpPrefixLSIdentifier;
33import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails;
34import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetailsLocalRib;
35import org.onosproject.bgpio.types.RouteDistinguisher;
36import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
Jonathan Hart51539b82015-10-29 09:53:04 -070039import java.util.Map;
40import java.util.Set;
Jonathan Hart51539b82015-10-29 09:53:04 -070041
Shashikanth VH3fe37982015-11-30 11:50:07 +053042import static com.google.common.base.Preconditions.checkNotNull;
Shashikanth VH3fe37982015-11-30 11:50:07 +053043
44/**
45 * Implementation of local RIB.
46 */
47public class BgpLocalRibImpl implements BgpLocalRib {
48
49 private static final Logger log = LoggerFactory.getLogger(BgpLocalRibImpl.class);
50 private BgpController bgpController;
51
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +000052 private final Map<BgpNodeLSIdentifier, PathAttrNlriDetailsLocalRib> nodeTree = Maps.newConcurrentMap();
53 private final Map<BgpLinkLSIdentifier, PathAttrNlriDetailsLocalRib> linkTree = Maps.newConcurrentMap();
54 private final Map<BgpPrefixLSIdentifier, PathAttrNlriDetailsLocalRib> prefixTree = Maps.newConcurrentMap();
Shashikanth VH3fe37982015-11-30 11:50:07 +053055
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +000056 private final Map<RouteDistinguisher, Map<BgpNodeLSIdentifier, PathAttrNlriDetailsLocalRib>> vpnNodeTree
57 = Maps.newConcurrentMap();
58 private final Map<RouteDistinguisher, Map<BgpLinkLSIdentifier, PathAttrNlriDetailsLocalRib>> vpnLinkTree
59 = Maps.newConcurrentMap();
60 private final Map<RouteDistinguisher, Map<BgpPrefixLSIdentifier, PathAttrNlriDetailsLocalRib>> vpnPrefixTree
61 = Maps.newConcurrentMap();
Shashikanth VH3fe37982015-11-30 11:50:07 +053062
63 public BgpLocalRibImpl(BgpController bgpController) {
64 this.bgpController = bgpController;
65 }
66
67 /**
68 * Gets node NLRI tree.
69 *
70 * @return node tree
71 */
mohamedrahil00f6f262016-11-24 20:20:41 +053072 @Override
Shashikanth VH3fe37982015-11-30 11:50:07 +053073 public Map<BgpNodeLSIdentifier, PathAttrNlriDetailsLocalRib> nodeTree() {
74 return nodeTree;
75 }
76
77 /**
78 * Gets link NLRI tree.
79 *
80 * @return link tree
81 */
mohamedrahil00f6f262016-11-24 20:20:41 +053082 @Override
Shashikanth VH3fe37982015-11-30 11:50:07 +053083 public Map<BgpLinkLSIdentifier, PathAttrNlriDetailsLocalRib> linkTree() {
84 return linkTree;
85 }
86
87 /**
88 * Gets prefix NLRI tree.
89 *
90 * @return prefix tree
91 */
mohamedrahil00f6f262016-11-24 20:20:41 +053092 @Override
Shashikanth VH3fe37982015-11-30 11:50:07 +053093 public Map<BgpPrefixLSIdentifier, PathAttrNlriDetailsLocalRib> prefixTree() {
94 return prefixTree;
95 }
96
97 /**
98 * Gets VPN node NLRI tree.
99 *
100 * @return vpn node NLRI tree
101 */
mohamedrahil00f6f262016-11-24 20:20:41 +0530102 @Override
Shashikanth VH3fe37982015-11-30 11:50:07 +0530103 public Map<RouteDistinguisher, Map<BgpNodeLSIdentifier, PathAttrNlriDetailsLocalRib>> vpnNodeTree() {
104 return vpnNodeTree;
105 }
106
107 /**
108 * Gets VPN link NLRI tree.
109 *
110 * @return vpn link NLRI Tree
111 */
mohamedrahil00f6f262016-11-24 20:20:41 +0530112 @Override
Shashikanth VH3fe37982015-11-30 11:50:07 +0530113 public Map<RouteDistinguisher, Map<BgpLinkLSIdentifier, PathAttrNlriDetailsLocalRib>> vpnLinkTree() {
114 return vpnLinkTree;
115 }
116
117 /**
118 * Gets VPN prefix NLRI tree.
119 *
120 * @return vpn prefix NLRI Tree
121 */
mohamedrahil00f6f262016-11-24 20:20:41 +0530122 @Override
Shashikanth VH3fe37982015-11-30 11:50:07 +0530123 public Map<RouteDistinguisher, Map<BgpPrefixLSIdentifier, PathAttrNlriDetailsLocalRib>> vpnPrefixTree() {
124 return vpnPrefixTree;
125 }
126
127 @Override
Priyanka Bfc51c952016-03-26 14:30:33 +0530128 public void add(BgpSessionInfo sessionInfo, BgpLSNlri nlri, PathAttrNlriDetails details) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530129 int decisionResult;
130
131 log.debug("Add to local RIB {}", details.toString());
132
133 PathAttrNlriDetailsLocalRib detailsLocRib = new PathAttrNlriDetailsLocalRib(
134 sessionInfo.remoteBgpId().ipAddress(),
135 sessionInfo.remoteBgpIdentifier(),
136 sessionInfo.remoteBgpASNum(),
137 sessionInfo.isIbgpSession(), details);
138 if (nlri instanceof BgpNodeLSNlriVer4) {
139 BgpNodeLSIdentifier nodeLsIdentifier = ((BgpNodeLSNlriVer4) nlri).getLocalNodeDescriptors();
140 if (nodeTree.containsKey(nodeLsIdentifier)) {
141 BgpSelectionAlgo selectionAlgo = new BgpSelectionAlgo();
142 // Compare local RIB entry with the current attribute
143 decisionResult = selectionAlgo.compare(nodeTree.get(nodeLsIdentifier), detailsLocRib);
Shashikanth VHe38c21e2016-05-26 21:37:25 +0530144 if (decisionResult <= 0) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530145 for (BgpNodeListener l : bgpController.listener()) {
146 l.addNode((BgpNodeLSNlriVer4) nlri, details);
147 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530148 nodeTree.replace(nodeLsIdentifier, detailsLocRib);
149 log.debug("Local RIB update node: {}", detailsLocRib.toString());
150 }
151 } else {
152 nodeTree.put(nodeLsIdentifier, detailsLocRib);
153 for (BgpNodeListener l : bgpController.listener()) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530154 l.addNode((BgpNodeLSNlriVer4) nlri, details);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530155 }
156 log.debug("Local RIB ad node: {}", detailsLocRib.toString());
157 }
158 } else if (nlri instanceof BgpLinkLsNlriVer4) {
159 BgpLinkLSIdentifier linkLsIdentifier = ((BgpLinkLsNlriVer4) nlri).getLinkIdentifier();
160 if (linkTree.containsKey(linkLsIdentifier)) {
161 BgpSelectionAlgo selectionAlgo = new BgpSelectionAlgo();
162 // Compare local RIB entry with the current attribute
163 decisionResult = selectionAlgo.compare(linkTree.get(linkLsIdentifier), detailsLocRib);
Shashikanth VHe38c21e2016-05-26 21:37:25 +0530164 if (decisionResult <= 0) {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530165 linkTree.replace(linkLsIdentifier, detailsLocRib);
Priyanka Bfc51c952016-03-26 14:30:33 +0530166 for (BgpLinkListener l : bgpController.linkListener()) {
167 l.addLink((BgpLinkLsNlriVer4) nlri, details);
168 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530169 log.debug("Local RIB update link: {}", detailsLocRib.toString());
170 }
171 } else {
172 linkTree.put(linkLsIdentifier, detailsLocRib);
Priyanka Bfc51c952016-03-26 14:30:33 +0530173 for (BgpLinkListener l : bgpController.linkListener()) {
174 l.addLink((BgpLinkLsNlriVer4) nlri, details);
175 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530176 log.debug("Local RIB add link: {}", detailsLocRib.toString());
177 }
178 } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) {
179 BgpPrefixLSIdentifier prefixIdentifier = ((BgpPrefixIPv4LSNlriVer4) nlri).getPrefixIdentifier();
180 if (prefixTree.containsKey(prefixIdentifier)) {
181 BgpSelectionAlgo selectionAlgo = new BgpSelectionAlgo();
182 // Compare local RIB entry with the current attribute
183 decisionResult = selectionAlgo.compare(prefixTree.get(prefixIdentifier), detailsLocRib);
Shashikanth VHe38c21e2016-05-26 21:37:25 +0530184 if (decisionResult <= 0) {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530185 prefixTree.replace(prefixIdentifier, detailsLocRib);
Maciej Skala3251a302019-03-26 09:16:55 +0100186 for (BgpPrefixListener l : bgpController.prefixListener()) {
187 l.addPrefix((BgpPrefixIPv4LSNlriVer4) nlri, details);
188 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530189 log.debug("Local RIB update prefix: {}", detailsLocRib.toString());
190 }
191 } else {
192 prefixTree.put(prefixIdentifier, detailsLocRib);
Maciej Skala3251a302019-03-26 09:16:55 +0100193 for (BgpPrefixListener l : bgpController.prefixListener()) {
194 l.addPrefix((BgpPrefixIPv4LSNlriVer4) nlri, details);
195 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530196 log.debug("Local RIB add prefix: {}", detailsLocRib.toString());
197 }
198 }
199 }
200
201 @Override
Priyanka Bfc51c952016-03-26 14:30:33 +0530202 public void delete(BgpLSNlri nlri) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530203 log.debug("Delete from local RIB.");
204
205 // Update local RIB
206 decisionProcess(nlri);
207 }
208
209 /**
210 * Update local RIB based on selection algorithm.
211 *
212 * @param nlri NLRI to update
Priyanka Bfc51c952016-03-26 14:30:33 +0530213 * @throws BgpParseException while updating to local RIB
Shashikanth VH3fe37982015-11-30 11:50:07 +0530214 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530215 public void decisionProcess(BgpLSNlri nlri) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530216 checkNotNull(nlri);
217 if (nlri instanceof BgpNodeLSNlriVer4) {
218 selectionProcessNode(nlri, false);
219 } else if (nlri instanceof BgpLinkLsNlriVer4) {
220 selectionProcessLink(nlri, false);
221 } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) {
222 selectionProcessPrefix(nlri, false);
223 }
224 }
225
226 /**
227 * Update VPN local RIB .
228 *
229 * @param nlri NLRI to update
230 * @param routeDistinguisher VPN id to update
Priyanka Bfc51c952016-03-26 14:30:33 +0530231 * @throws BgpParseException BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530232 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530233 public void decisionProcess(BgpLSNlri nlri, RouteDistinguisher routeDistinguisher) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530234 checkNotNull(nlri);
235 if (nlri instanceof BgpNodeLSNlriVer4) {
236 if (vpnNodeTree.containsKey(routeDistinguisher)) {
237 selectionProcessNode(nlri, true);
238 if (nodeTree.size() == 0) {
239 vpnNodeTree.remove(routeDistinguisher);
240 }
241 }
242 } else if (nlri instanceof BgpLinkLsNlriVer4) {
243 if (vpnLinkTree.containsKey(routeDistinguisher)) {
244 selectionProcessLink(nlri, true);
245 if (linkTree.size() == 0) {
246 vpnLinkTree.remove(routeDistinguisher);
247 }
248 }
249 } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) {
250 if (vpnPrefixTree.containsKey(routeDistinguisher)) {
251 selectionProcessPrefix(nlri, true);
252 if (prefixTree.size() == 0) {
253 vpnPrefixTree.remove(routeDistinguisher);
254 }
255 }
256 }
257 }
258
259 /**
260 * Selection process for local RIB node.
261 *
262 * @param nlri NLRI to update
263 * @param isVpnRib true if VPN local RIB, otherwise false
Priyanka Bfc51c952016-03-26 14:30:33 +0530264 * @throws BgpParseException throws BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530265 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530266 public void selectionProcessNode(BgpLSNlri nlri, boolean isVpnRib) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530267 BgpPeerImpl peer;
268 BgpSessionInfo sessionInfo;
269 int decisionResult;
270 boolean containsKey;
271
272 BgpNodeLSIdentifier nodeLsIdentifier = ((BgpNodeLSNlriVer4) nlri).getLocalNodeDescriptors();
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +0000273 /* Here, we are checking if the given node is contained in the AdjacencyRib of any peer
274 or not. If none of the peer's AdjacencyRib has it, node can be marked for deletion.
275 */
276 boolean shouldDeleteNode = false;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530277 if (nodeTree.containsKey(nodeLsIdentifier)) {
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +0000278 shouldDeleteNode = true;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530279 }
280
281 for (BgpId bgpId : bgpController.connectedPeers().keySet()) {
282 peer = (BgpPeerImpl) (bgpController.getPeer(bgpId));
283
284 if (nodeTree.containsKey(nodeLsIdentifier)) {
285 containsKey = (!isVpnRib) ? (peer.adjacencyRib().nodeTree().containsKey(nodeLsIdentifier)) :
286 (peer.vpnAdjacencyRib().nodeTree().containsKey(nodeLsIdentifier));
287
288 if (!containsKey) {
289 continue;
290 }
291 sessionInfo = peer.sessionInfo();
292 PathAttrNlriDetailsLocalRib detailsLocRib = new PathAttrNlriDetailsLocalRib(
293 sessionInfo.remoteBgpId().ipAddress(),
294 sessionInfo.remoteBgpIdentifier(),
295 sessionInfo.remoteBgpASNum(),
296 sessionInfo.isIbgpSession(),
297 (!isVpnRib) ?
298 (peer.adjacencyRib().nodeTree()
299 .get(nodeLsIdentifier)) :
300 (peer.vpnAdjacencyRib().nodeTree()
301 .get(nodeLsIdentifier)));
302 BgpSelectionAlgo selectionAlgo = new BgpSelectionAlgo();
303 decisionResult = selectionAlgo.compare(nodeTree.get(nodeLsIdentifier), detailsLocRib);
304 if (decisionResult < 0) {
305 nodeTree.replace(nodeLsIdentifier, detailsLocRib);
306 log.debug("Local RIB node updated: {}", detailsLocRib.toString());
307 }
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +0000308 shouldDeleteNode = false;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530309 }
310 }
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +0000311 if (shouldDeleteNode) {
312 log.debug("Local RIB delete node: {}", nodeLsIdentifier.toString());
313 for (BgpNodeListener l : bgpController.listener()) {
314 l.deleteNode((BgpNodeLSNlriVer4) nlri);
315 }
316 nodeTree.remove(nodeLsIdentifier);
317 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530318 }
319
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +0000320 /**
Shashikanth VH3fe37982015-11-30 11:50:07 +0530321 * Selection process for local RIB link.
322 *
323 * @param nlri NLRI to update
324 * @param isVpnRib true if VPN local RIB, otherwise false
Priyanka Bfc51c952016-03-26 14:30:33 +0530325 * @throws BgpParseException BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530326 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530327 public void selectionProcessLink(BgpLSNlri nlri, boolean isVpnRib) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530328 BgpPeerImpl peer;
329 BgpSessionInfo sessionInfo;
330 int decisionResult;
331 boolean containsKey;
332
333 BgpLinkLSIdentifier linkLsIdentifier = ((BgpLinkLsNlriVer4) nlri).getLinkIdentifier();
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +0000334 /* Here, we are checking if the given link is contained in the AdjacencyRib of any peer
335 or not. If none of the peer's AdjacencyRib has it, link can be marked for deletion.
336 */
337 boolean shouldDeleteLink = false;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530338 if (linkTree.containsKey(linkLsIdentifier)) {
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +0000339 shouldDeleteLink = true;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530340 }
341
342 for (BgpId bgpId : bgpController.connectedPeers().keySet()) {
343 peer = (BgpPeerImpl) (bgpController.getPeer(bgpId));
344
345 if (linkTree.containsKey(linkLsIdentifier)) {
346
347 containsKey = (!isVpnRib) ? (peer.adjacencyRib().linkTree().containsKey(linkLsIdentifier)) :
348 (peer.vpnAdjacencyRib().linkTree().containsKey(linkLsIdentifier));
349
350 if (!containsKey) {
351 continue;
352 }
353
354 sessionInfo = peer.sessionInfo();
355
356 PathAttrNlriDetailsLocalRib detailsLocRib = new PathAttrNlriDetailsLocalRib(
357 sessionInfo.remoteBgpId().ipAddress(),
358 sessionInfo.remoteBgpIdentifier(),
359 sessionInfo.remoteBgpASNum(),
360 sessionInfo.isIbgpSession(),
361 ((!isVpnRib) ?
362 (peer.adjacencyRib().linkTree().get(linkLsIdentifier)) :
363 (peer.vpnAdjacencyRib().linkTree()
364 .get(linkLsIdentifier))));
365
366 BgpSelectionAlgo selectionAlgo = new BgpSelectionAlgo();
367 decisionResult = selectionAlgo.compare(linkTree.get(linkLsIdentifier), detailsLocRib);
368 if (decisionResult < 0) {
369 linkTree.replace(linkLsIdentifier, detailsLocRib);
370 log.debug("Local RIB link updated: {}", detailsLocRib.toString());
371 }
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +0000372 shouldDeleteLink = false;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530373 }
374 }
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +0000375
376 if (shouldDeleteLink) {
377 log.debug("Local RIB remove link: {}", linkLsIdentifier.toString());
378 for (BgpLinkListener l : bgpController.linkListener()) {
379 l.deleteLink((BgpLinkLsNlriVer4) nlri);
380 }
381 linkTree.remove(linkLsIdentifier);
382
383 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530384 }
385
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +0000386 /**
Shashikanth VH3fe37982015-11-30 11:50:07 +0530387 * Selection process for local RIB prefix.
388 *
389 * @param nlri NLRI to update
390 * @param isVpnRib true if VPN local RIB, otherwise false
Priyanka Bfc51c952016-03-26 14:30:33 +0530391 * @throws BgpParseException BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530392 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530393 public void selectionProcessPrefix(BgpLSNlri nlri, boolean isVpnRib) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530394 BgpPeerImpl peer;
395 BgpSessionInfo sessionInfo;
396 int decisionResult;
397 boolean containsKey;
398
399 BgpPrefixLSIdentifier prefixIdentifier = ((BgpPrefixIPv4LSNlriVer4) nlri).getPrefixIdentifier();
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +0000400 /* Here, we are checking if the given prefix is contained in the AdjacencyRib of any peer
401 or not. If none of the peer's AdjacencyRib has it, prefix can be marked for deletion.
402 */
403 boolean shouldDeletePrefix = false;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530404 if (prefixTree.containsKey(prefixIdentifier)) {
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +0000405 shouldDeletePrefix = true;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530406 }
407
408 for (BgpId bgpId : bgpController.connectedPeers().keySet()) {
409 peer = (BgpPeerImpl) (bgpController.getPeer(bgpId));
410
411 if (prefixTree.containsKey(prefixIdentifier)) {
412
413 containsKey = (!isVpnRib) ? (peer.adjacencyRib().prefixTree().containsKey(prefixIdentifier)) :
414 (peer.vpnAdjacencyRib().prefixTree().containsKey(prefixIdentifier));
415 if (!containsKey) {
416 continue;
417 }
418 sessionInfo = peer.sessionInfo();
419
420 PathAttrNlriDetailsLocalRib detailsLocRib = new PathAttrNlriDetailsLocalRib(
421 sessionInfo.remoteBgpId().ipAddress(),
422 sessionInfo.remoteBgpIdentifier(),
423 sessionInfo.remoteBgpASNum(),
424 sessionInfo.isIbgpSession(),
425 ((!isVpnRib) ?
426 (peer.adjacencyRib().prefixTree()
427 .get(prefixIdentifier)) :
428 (peer.vpnAdjacencyRib().prefixTree()
429 .get(prefixIdentifier))));
430
431 BgpSelectionAlgo selectionAlgo = new BgpSelectionAlgo();
432 decisionResult = selectionAlgo.compare(prefixTree.get(prefixIdentifier), detailsLocRib);
433 if (decisionResult < 0) {
434 prefixTree.replace(prefixIdentifier, detailsLocRib);
mohamedrahil00f6f262016-11-24 20:20:41 +0530435 log.debug("Local RIB prefix updated: {}", detailsLocRib.toString());
Shashikanth VH3fe37982015-11-30 11:50:07 +0530436 }
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +0000437 shouldDeletePrefix = false;
438 }
439 if (shouldDeletePrefix) {
440 log.debug("Local RIB remove prefix: {}", prefixIdentifier.toString());
441 for (BgpPrefixListener l : bgpController.prefixListener()) {
442 l.deletePrefix((BgpPrefixIPv4LSNlriVer4) nlri);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530443 }
ChinmayaAgarwala3d7d7e2020-02-06 08:23:30 +0000444 prefixTree.remove(prefixIdentifier);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530445 }
446 }
447 }
448
449 @Override
450 public void add(BgpSessionInfo sessionInfo, BgpLSNlri nlri, PathAttrNlriDetails details,
Priyanka Bfc51c952016-03-26 14:30:33 +0530451 RouteDistinguisher routeDistinguisher) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530452 add(sessionInfo, nlri, details);
453 if (nlri instanceof BgpNodeLSNlriVer4) {
454 if (!vpnNodeTree.containsKey(routeDistinguisher)) {
455 vpnNodeTree.put(routeDistinguisher, nodeTree);
456 }
457 } else if (nlri instanceof BgpLinkLsNlriVer4) {
458 if (!vpnLinkTree.containsKey(routeDistinguisher)) {
459 vpnLinkTree.put(routeDistinguisher, linkTree);
460 }
461 } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) {
462 if (!vpnPrefixTree.containsKey(routeDistinguisher)) {
463 vpnPrefixTree.put(routeDistinguisher, prefixTree);
464 }
465 }
466 }
467
468 @Override
Priyanka Bfc51c952016-03-26 14:30:33 +0530469 public void delete(BgpLSNlri nlri, RouteDistinguisher routeDistinguisher) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530470 // Update local RIB
471 decisionProcess(nlri, routeDistinguisher);
472 }
473
474 /**
Ray Milkeyc108a6b2017-08-23 15:23:50 -0700475 * Update local RIB node based on available peer adjacency RIB.
Shashikanth VH3fe37982015-11-30 11:50:07 +0530476 *
477 * @param o adjacency-in/VPN adjacency-in
Priyanka Bfc51c952016-03-26 14:30:33 +0530478 * @throws BgpParseException BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530479 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530480 public void localRibUpdateNode(Object o) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530481
482 if (o instanceof AdjRibIn) {
483 AdjRibIn adjRib = (AdjRibIn) o;
484 log.debug("Update local RIB node.");
485
486 Set<BgpNodeLSIdentifier> nodeKeys = adjRib.nodeTree().keySet();
487 for (BgpNodeLSIdentifier key : nodeKeys) {
488 PathAttrNlriDetails pathAttrNlri = adjRib.nodeTree().get(key);
489
490 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(pathAttrNlri.identifier(), pathAttrNlri
491 .protocolID().getType(), key, false, null);
492 decisionProcess(nodeNlri);
493 }
494 }
495
496 if (o instanceof VpnAdjRibIn) {
497 VpnAdjRibIn vpnAdjRib = (VpnAdjRibIn) o;
498 log.debug("Update local RIB VPN node.");
499 Set<RouteDistinguisher> nodeKeysVpn = vpnAdjRib.vpnNodeTree().keySet();
500 Map<BgpNodeLSIdentifier, PathAttrNlriDetails> node;
501 for (RouteDistinguisher keyVpnNode : nodeKeysVpn) {
502 node = vpnAdjRib.vpnNodeTree().get(keyVpnNode);
503
504 Set<BgpNodeLSIdentifier> vpnNodeKeys = node.keySet();
505 for (BgpNodeLSIdentifier key : vpnNodeKeys) {
506 PathAttrNlriDetails pathAttrNlri = vpnAdjRib.nodeTree().get(key);
507 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(pathAttrNlri.identifier(),
508 pathAttrNlri.protocolID().getType(),
509 key, true, keyVpnNode);
510 decisionProcess(nodeNlri, keyVpnNode);
511 }
512 }
513 }
514 }
515
516 /**
Ray Milkeyc108a6b2017-08-23 15:23:50 -0700517 * Update localRIB link based on available peer adjacency RIB.
Shashikanth VH3fe37982015-11-30 11:50:07 +0530518 *
519 * @param o adjacency-in/VPN adjacency-in
Priyanka Bfc51c952016-03-26 14:30:33 +0530520 * @throws BgpParseException BGP parse exceptions
Shashikanth VH3fe37982015-11-30 11:50:07 +0530521 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530522 public void localRibUpdateLink(Object o) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530523
524 if (o instanceof AdjRibIn) {
525 AdjRibIn adjRib = (AdjRibIn) o;
526 log.debug("Update local RIB link.");
527
528 Set<BgpLinkLSIdentifier> linkKeys = adjRib.linkTree().keySet();
529 for (BgpLinkLSIdentifier key : linkKeys) {
530 PathAttrNlriDetails pathAttrNlri = adjRib.linkTree().get(key);
531 BgpLinkLsNlriVer4 linkNlri = new BgpLinkLsNlriVer4(pathAttrNlri.protocolID().getType(),
532 pathAttrNlri.identifier(), key, null, false);
533 decisionProcess(linkNlri);
534 }
535 }
536
537 if (o instanceof VpnAdjRibIn) {
538 VpnAdjRibIn vpnAdjRib = (VpnAdjRibIn) o;
539 log.debug("Update local RIB VPN link");
540
541 Set<RouteDistinguisher> linkKeysVpn = vpnAdjRib.vpnLinkTree().keySet();
542 Map<BgpLinkLSIdentifier, PathAttrNlriDetails> link;
543 for (RouteDistinguisher keyVpnLink : linkKeysVpn) {
544 link = vpnAdjRib.vpnLinkTree().get(keyVpnLink);
545
546 Set<BgpLinkLSIdentifier> vpnLinkKeys = link.keySet();
547 for (BgpLinkLSIdentifier key : vpnLinkKeys) {
548 PathAttrNlriDetails pathAttrNlri = vpnAdjRib.linkTree().get(key);
549 BgpLinkLsNlriVer4 linkNlri = new BgpLinkLsNlriVer4(pathAttrNlri.protocolID().getType(),
550 pathAttrNlri.identifier(), key, keyVpnLink,
551 true);
552 decisionProcess(linkNlri, keyVpnLink);
553 }
554 }
555 }
556 }
557
558 /**
Ray Milkeyc108a6b2017-08-23 15:23:50 -0700559 * Update localRIB prefix based on available peer adjacency RIB.
Shashikanth VH3fe37982015-11-30 11:50:07 +0530560 *
561 * @param o instance of adjacency-in/VPN adjacency-in
Priyanka Bfc51c952016-03-26 14:30:33 +0530562 * @throws BgpParseException BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530563 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530564 public void localRibUpdatePrefix(Object o) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530565
566 if (o instanceof AdjRibIn) {
567 AdjRibIn adjRib = (AdjRibIn) o;
568 log.debug("Update local RIB prefix.");
569
570 Set<BgpPrefixLSIdentifier> prefixKeys = adjRib.prefixTree().keySet();
571 for (BgpPrefixLSIdentifier key : prefixKeys) {
572 PathAttrNlriDetails pathAttrNlri = adjRib.prefixTree().get(key);
573 BgpPrefixIPv4LSNlriVer4 prefixNlri = new BgpPrefixIPv4LSNlriVer4(
574 pathAttrNlri.identifier(),
575 pathAttrNlri.protocolID().getType(),
576 key, null, false);
577 decisionProcess(prefixNlri);
578 }
579 }
580
581 if (o instanceof VpnAdjRibIn) {
582 VpnAdjRibIn vpnAdjRib = (VpnAdjRibIn) o;
583 log.debug("Update local RIB VPN prefix.");
584
585 Set<RouteDistinguisher> prefixKeysVpn = vpnAdjRib.vpnPrefixTree().keySet();
586 Map<BgpPrefixLSIdentifier, PathAttrNlriDetails> prefix;
587 for (RouteDistinguisher keyVpnPrefix : prefixKeysVpn) {
588 prefix = vpnAdjRib.vpnPrefixTree().get(keyVpnPrefix);
589
590 Set<BgpPrefixLSIdentifier> vpnPrefixKeys = prefix.keySet();
591 for (BgpPrefixLSIdentifier key : vpnPrefixKeys) {
592 PathAttrNlriDetails pathAttrNlri = vpnAdjRib.prefixTree().get(key);
593 BgpPrefixIPv4LSNlriVer4 prefixNlri = new BgpPrefixIPv4LSNlriVer4(pathAttrNlri.identifier(),
594 pathAttrNlri.protocolID()
595 .getType(), key,
596 keyVpnPrefix, true);
597 decisionProcess(prefixNlri, keyVpnPrefix);
598 }
599 }
600 }
601 }
602
603 /**
604 * Update localRIB.
605 *
606 * @param adjRibIn adjacency RIB-in
Priyanka Bfc51c952016-03-26 14:30:33 +0530607 * @throws BgpParseException BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530608 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530609 public void localRibUpdate(AdjRibIn adjRibIn) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530610 log.debug("Update local RIB.");
611
Jonathan Hart51539b82015-10-29 09:53:04 -0700612 localRibUpdateNode(adjRibIn);
613 localRibUpdateLink(adjRibIn);
614 localRibUpdatePrefix(adjRibIn);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530615 }
616
617 /**
618 * Update localRIB.
619 *
620 * @param vpnAdjRibIn VPN adjacency RIB-in
Priyanka Bfc51c952016-03-26 14:30:33 +0530621 * @throws BgpParseException BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530622 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530623 public void localRibUpdate(VpnAdjRibIn vpnAdjRibIn) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530624 log.debug("Update VPN local RIB.");
625
Jonathan Hart51539b82015-10-29 09:53:04 -0700626 localRibUpdateNode(vpnAdjRibIn);
627 localRibUpdateLink(vpnAdjRibIn);
628 localRibUpdatePrefix(vpnAdjRibIn);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530629 }
630
631 @Override
632 public String toString() {
633 return MoreObjects.toStringHelper(getClass()).omitNullValues().add("nodeTree", nodeTree)
634 .add("linkTree", linkTree).add("prefixTree", prefixTree).add("vpnNodeTree", vpnNodeTree)
635 .add("vpnLinkTree", vpnLinkTree).add("vpnPrefixTree", vpnPrefixTree).toString();
636 }
637}