blob: 9bf2ee388c89cfd4717ac9388c665cf909767fc5 [file] [log] [blame]
Shashikanth VH3fe37982015-11-30 11:50:07 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
Jonathan Hart51539b82015-10-29 09:53:04 -070016import com.google.common.base.MoreObjects;
Priyanka Bfc51c952016-03-26 14:30:33 +053017
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;
23import org.onosproject.bgp.controller.BgpSessionInfo;
Priyanka Bfc51c952016-03-26 14:30:33 +053024import org.onosproject.bgpio.exceptions.BgpParseException;
Shashikanth VH3fe37982015-11-30 11:50:07 +053025import org.onosproject.bgpio.protocol.BgpLSNlri;
26import org.onosproject.bgpio.protocol.linkstate.BgpLinkLSIdentifier;
27import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
28import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier;
29import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
30import org.onosproject.bgpio.protocol.linkstate.BgpPrefixIPv4LSNlriVer4;
31import org.onosproject.bgpio.protocol.linkstate.BgpPrefixLSIdentifier;
32import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails;
33import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetailsLocalRib;
34import org.onosproject.bgpio.types.RouteDistinguisher;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
Jonathan Hart51539b82015-10-29 09:53:04 -070038import java.util.Map;
39import java.util.Set;
40import java.util.TreeMap;
41
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
52 private Map<BgpNodeLSIdentifier, PathAttrNlriDetailsLocalRib> nodeTree = new TreeMap<>();
53 private Map<BgpLinkLSIdentifier, PathAttrNlriDetailsLocalRib> linkTree = new TreeMap<>();
54 private Map<BgpPrefixLSIdentifier, PathAttrNlriDetailsLocalRib> prefixTree = new TreeMap<>();
55
56 private Map<RouteDistinguisher, Map<BgpNodeLSIdentifier, PathAttrNlriDetailsLocalRib>> vpnNodeTree
57 = new TreeMap<>();
58 private Map<RouteDistinguisher, Map<BgpLinkLSIdentifier, PathAttrNlriDetailsLocalRib>> vpnLinkTree
59 = new TreeMap<>();
60 private Map<RouteDistinguisher, Map<BgpPrefixLSIdentifier, PathAttrNlriDetailsLocalRib>> vpnPrefixTree
61 = new TreeMap<>();
62
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);
186 log.debug("Local RIB update prefix: {}", detailsLocRib.toString());
187 }
188 } else {
189 prefixTree.put(prefixIdentifier, detailsLocRib);
190 log.debug("Local RIB add prefix: {}", detailsLocRib.toString());
191 }
192 }
193 }
194
195 @Override
Priyanka Bfc51c952016-03-26 14:30:33 +0530196 public void delete(BgpLSNlri nlri) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530197 log.debug("Delete from local RIB.");
198
199 // Update local RIB
200 decisionProcess(nlri);
201 }
202
203 /**
204 * Update local RIB based on selection algorithm.
205 *
206 * @param nlri NLRI to update
Priyanka Bfc51c952016-03-26 14:30:33 +0530207 * @throws BgpParseException while updating to local RIB
Shashikanth VH3fe37982015-11-30 11:50:07 +0530208 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530209 public void decisionProcess(BgpLSNlri nlri) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530210 checkNotNull(nlri);
211 if (nlri instanceof BgpNodeLSNlriVer4) {
212 selectionProcessNode(nlri, false);
213 } else if (nlri instanceof BgpLinkLsNlriVer4) {
214 selectionProcessLink(nlri, false);
215 } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) {
216 selectionProcessPrefix(nlri, false);
217 }
218 }
219
220 /**
221 * Update VPN local RIB .
222 *
223 * @param nlri NLRI to update
224 * @param routeDistinguisher VPN id to update
Priyanka Bfc51c952016-03-26 14:30:33 +0530225 * @throws BgpParseException BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530226 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530227 public void decisionProcess(BgpLSNlri nlri, RouteDistinguisher routeDistinguisher) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530228 checkNotNull(nlri);
229 if (nlri instanceof BgpNodeLSNlriVer4) {
230 if (vpnNodeTree.containsKey(routeDistinguisher)) {
231 selectionProcessNode(nlri, true);
232 if (nodeTree.size() == 0) {
233 vpnNodeTree.remove(routeDistinguisher);
234 }
235 }
236 } else if (nlri instanceof BgpLinkLsNlriVer4) {
237 if (vpnLinkTree.containsKey(routeDistinguisher)) {
238 selectionProcessLink(nlri, true);
239 if (linkTree.size() == 0) {
240 vpnLinkTree.remove(routeDistinguisher);
241 }
242 }
243 } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) {
244 if (vpnPrefixTree.containsKey(routeDistinguisher)) {
245 selectionProcessPrefix(nlri, true);
246 if (prefixTree.size() == 0) {
247 vpnPrefixTree.remove(routeDistinguisher);
248 }
249 }
250 }
251 }
252
253 /**
254 * Selection process for local RIB node.
255 *
256 * @param nlri NLRI to update
257 * @param isVpnRib true if VPN local RIB, otherwise false
Priyanka Bfc51c952016-03-26 14:30:33 +0530258 * @throws BgpParseException throws BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530259 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530260 public void selectionProcessNode(BgpLSNlri nlri, boolean isVpnRib) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530261 BgpPeerImpl peer;
262 BgpSessionInfo sessionInfo;
263 int decisionResult;
264 boolean containsKey;
265
266 BgpNodeLSIdentifier nodeLsIdentifier = ((BgpNodeLSNlriVer4) nlri).getLocalNodeDescriptors();
267
268 if (nodeTree.containsKey(nodeLsIdentifier)) {
269 for (BgpNodeListener l : bgpController.listener()) {
270 l.deleteNode((BgpNodeLSNlriVer4) nlri);
271 }
272 log.debug("Local RIB delete node: {}", nodeLsIdentifier.toString());
273 nodeTree.remove(nodeLsIdentifier);
274 }
275
276 for (BgpId bgpId : bgpController.connectedPeers().keySet()) {
277 peer = (BgpPeerImpl) (bgpController.getPeer(bgpId));
278
279 if (nodeTree.containsKey(nodeLsIdentifier)) {
280 containsKey = (!isVpnRib) ? (peer.adjacencyRib().nodeTree().containsKey(nodeLsIdentifier)) :
281 (peer.vpnAdjacencyRib().nodeTree().containsKey(nodeLsIdentifier));
282
283 if (!containsKey) {
284 continue;
285 }
286 sessionInfo = peer.sessionInfo();
287 PathAttrNlriDetailsLocalRib detailsLocRib = new PathAttrNlriDetailsLocalRib(
288 sessionInfo.remoteBgpId().ipAddress(),
289 sessionInfo.remoteBgpIdentifier(),
290 sessionInfo.remoteBgpASNum(),
291 sessionInfo.isIbgpSession(),
292 (!isVpnRib) ?
293 (peer.adjacencyRib().nodeTree()
294 .get(nodeLsIdentifier)) :
295 (peer.vpnAdjacencyRib().nodeTree()
296 .get(nodeLsIdentifier)));
297 BgpSelectionAlgo selectionAlgo = new BgpSelectionAlgo();
298 decisionResult = selectionAlgo.compare(nodeTree.get(nodeLsIdentifier), detailsLocRib);
299 if (decisionResult < 0) {
300 nodeTree.replace(nodeLsIdentifier, detailsLocRib);
301 log.debug("Local RIB node updated: {}", detailsLocRib.toString());
302 }
303 } else {
304 if (!isVpnRib) {
305 if (peer.adjacencyRib().nodeTree().containsKey(nodeLsIdentifier)) {
306 add(peer.sessionInfo(), nlri, peer.adjacencyRib().nodeTree().get(nodeLsIdentifier));
307 }
308 } else {
309 if (peer.vpnAdjacencyRib().nodeTree().containsKey(nodeLsIdentifier)) {
310 add(peer.sessionInfo(), nlri, peer.vpnAdjacencyRib().nodeTree().get(nodeLsIdentifier));
311 }
312 }
313 }
314 }
315 }
316
317 /**
318 * Selection process for local RIB link.
319 *
320 * @param nlri NLRI to update
321 * @param isVpnRib true if VPN local RIB, otherwise false
Priyanka Bfc51c952016-03-26 14:30:33 +0530322 * @throws BgpParseException BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530323 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530324 public void selectionProcessLink(BgpLSNlri nlri, boolean isVpnRib) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530325 BgpPeerImpl peer;
326 BgpSessionInfo sessionInfo;
327 int decisionResult;
328 boolean containsKey;
329
330 BgpLinkLSIdentifier linkLsIdentifier = ((BgpLinkLsNlriVer4) nlri).getLinkIdentifier();
331
332 if (linkTree.containsKey(linkLsIdentifier)) {
333 log.debug("Local RIB remove link: {}", linkLsIdentifier.toString());
Priyanka Bfc51c952016-03-26 14:30:33 +0530334 for (BgpLinkListener l : bgpController.linkListener()) {
335 l.deleteLink((BgpLinkLsNlriVer4) nlri);
336 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530337 linkTree.remove(linkLsIdentifier);
338 }
339
340 for (BgpId bgpId : bgpController.connectedPeers().keySet()) {
341 peer = (BgpPeerImpl) (bgpController.getPeer(bgpId));
342
343 if (linkTree.containsKey(linkLsIdentifier)) {
344
345 containsKey = (!isVpnRib) ? (peer.adjacencyRib().linkTree().containsKey(linkLsIdentifier)) :
346 (peer.vpnAdjacencyRib().linkTree().containsKey(linkLsIdentifier));
347
348 if (!containsKey) {
349 continue;
350 }
351
352 sessionInfo = peer.sessionInfo();
353
354 PathAttrNlriDetailsLocalRib detailsLocRib = new PathAttrNlriDetailsLocalRib(
355 sessionInfo.remoteBgpId().ipAddress(),
356 sessionInfo.remoteBgpIdentifier(),
357 sessionInfo.remoteBgpASNum(),
358 sessionInfo.isIbgpSession(),
359 ((!isVpnRib) ?
360 (peer.adjacencyRib().linkTree().get(linkLsIdentifier)) :
361 (peer.vpnAdjacencyRib().linkTree()
362 .get(linkLsIdentifier))));
363
364 BgpSelectionAlgo selectionAlgo = new BgpSelectionAlgo();
365 decisionResult = selectionAlgo.compare(linkTree.get(linkLsIdentifier), detailsLocRib);
366 if (decisionResult < 0) {
367 linkTree.replace(linkLsIdentifier, detailsLocRib);
368 log.debug("Local RIB link updated: {}", detailsLocRib.toString());
369 }
370 } else {
371 if (!isVpnRib) {
372 if (peer.adjacencyRib().linkTree().containsKey(linkLsIdentifier)) {
373 add(peer.sessionInfo(), nlri, peer.adjacencyRib().linkTree().get(linkLsIdentifier));
374 }
375 } else {
376 if (peer.vpnAdjacencyRib().linkTree().containsKey(linkLsIdentifier)) {
377 add(peer.sessionInfo(), nlri, peer.vpnAdjacencyRib().linkTree().get(linkLsIdentifier));
378 }
379 }
380 }
381 }
382 }
383
384 /**
385 * Selection process for local RIB prefix.
386 *
387 * @param nlri NLRI to update
388 * @param isVpnRib true if VPN local RIB, otherwise false
Priyanka Bfc51c952016-03-26 14:30:33 +0530389 * @throws BgpParseException BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530390 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530391 public void selectionProcessPrefix(BgpLSNlri nlri, boolean isVpnRib) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530392 BgpPeerImpl peer;
393 BgpSessionInfo sessionInfo;
394 int decisionResult;
395 boolean containsKey;
396
397 BgpPrefixLSIdentifier prefixIdentifier = ((BgpPrefixIPv4LSNlriVer4) nlri).getPrefixIdentifier();
398 if (prefixTree.containsKey(prefixIdentifier)) {
399 log.debug("Local RIB remove prefix: {}", prefixIdentifier.toString());
400 prefixTree.remove(prefixIdentifier);
401 }
402
403 for (BgpId bgpId : bgpController.connectedPeers().keySet()) {
404 peer = (BgpPeerImpl) (bgpController.getPeer(bgpId));
405
406 if (prefixTree.containsKey(prefixIdentifier)) {
407
408 containsKey = (!isVpnRib) ? (peer.adjacencyRib().prefixTree().containsKey(prefixIdentifier)) :
409 (peer.vpnAdjacencyRib().prefixTree().containsKey(prefixIdentifier));
410 if (!containsKey) {
411 continue;
412 }
413 sessionInfo = peer.sessionInfo();
414
415 PathAttrNlriDetailsLocalRib detailsLocRib = new PathAttrNlriDetailsLocalRib(
416 sessionInfo.remoteBgpId().ipAddress(),
417 sessionInfo.remoteBgpIdentifier(),
418 sessionInfo.remoteBgpASNum(),
419 sessionInfo.isIbgpSession(),
420 ((!isVpnRib) ?
421 (peer.adjacencyRib().prefixTree()
422 .get(prefixIdentifier)) :
423 (peer.vpnAdjacencyRib().prefixTree()
424 .get(prefixIdentifier))));
425
426 BgpSelectionAlgo selectionAlgo = new BgpSelectionAlgo();
427 decisionResult = selectionAlgo.compare(prefixTree.get(prefixIdentifier), detailsLocRib);
428 if (decisionResult < 0) {
429 prefixTree.replace(prefixIdentifier, detailsLocRib);
mohamedrahil00f6f262016-11-24 20:20:41 +0530430 log.debug("Local RIB prefix updated: {}", detailsLocRib.toString());
Shashikanth VH3fe37982015-11-30 11:50:07 +0530431 }
432 } else {
433 if (!isVpnRib) {
434 if (peer.adjacencyRib().prefixTree().containsKey(prefixIdentifier)) {
435 add(peer.sessionInfo(), nlri, peer.adjacencyRib().prefixTree().get(prefixIdentifier));
436 } else {
437 if (peer.vpnAdjacencyRib().prefixTree().containsKey(prefixIdentifier)) {
438 add(peer.sessionInfo(), nlri, peer.vpnAdjacencyRib().prefixTree().get(prefixIdentifier));
439 }
440 }
441 }
442 }
443 }
444 }
445
446 @Override
447 public void add(BgpSessionInfo sessionInfo, BgpLSNlri nlri, PathAttrNlriDetails details,
Priyanka Bfc51c952016-03-26 14:30:33 +0530448 RouteDistinguisher routeDistinguisher) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530449 add(sessionInfo, nlri, details);
450 if (nlri instanceof BgpNodeLSNlriVer4) {
451 if (!vpnNodeTree.containsKey(routeDistinguisher)) {
452 vpnNodeTree.put(routeDistinguisher, nodeTree);
453 }
454 } else if (nlri instanceof BgpLinkLsNlriVer4) {
455 if (!vpnLinkTree.containsKey(routeDistinguisher)) {
456 vpnLinkTree.put(routeDistinguisher, linkTree);
457 }
458 } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) {
459 if (!vpnPrefixTree.containsKey(routeDistinguisher)) {
460 vpnPrefixTree.put(routeDistinguisher, prefixTree);
461 }
462 }
463 }
464
465 @Override
Priyanka Bfc51c952016-03-26 14:30:33 +0530466 public void delete(BgpLSNlri nlri, RouteDistinguisher routeDistinguisher) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530467 // Update local RIB
468 decisionProcess(nlri, routeDistinguisher);
469 }
470
471 /**
472 * Update local RIB node based on avaliable peer adjacency RIB.
473 *
474 * @param o adjacency-in/VPN adjacency-in
Priyanka Bfc51c952016-03-26 14:30:33 +0530475 * @throws BgpParseException BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530476 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530477 public void localRibUpdateNode(Object o) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530478
479 if (o instanceof AdjRibIn) {
480 AdjRibIn adjRib = (AdjRibIn) o;
481 log.debug("Update local RIB node.");
482
483 Set<BgpNodeLSIdentifier> nodeKeys = adjRib.nodeTree().keySet();
484 for (BgpNodeLSIdentifier key : nodeKeys) {
485 PathAttrNlriDetails pathAttrNlri = adjRib.nodeTree().get(key);
486
487 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(pathAttrNlri.identifier(), pathAttrNlri
488 .protocolID().getType(), key, false, null);
489 decisionProcess(nodeNlri);
490 }
491 }
492
493 if (o instanceof VpnAdjRibIn) {
494 VpnAdjRibIn vpnAdjRib = (VpnAdjRibIn) o;
495 log.debug("Update local RIB VPN node.");
496 Set<RouteDistinguisher> nodeKeysVpn = vpnAdjRib.vpnNodeTree().keySet();
497 Map<BgpNodeLSIdentifier, PathAttrNlriDetails> node;
498 for (RouteDistinguisher keyVpnNode : nodeKeysVpn) {
499 node = vpnAdjRib.vpnNodeTree().get(keyVpnNode);
500
501 Set<BgpNodeLSIdentifier> vpnNodeKeys = node.keySet();
502 for (BgpNodeLSIdentifier key : vpnNodeKeys) {
503 PathAttrNlriDetails pathAttrNlri = vpnAdjRib.nodeTree().get(key);
504 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(pathAttrNlri.identifier(),
505 pathAttrNlri.protocolID().getType(),
506 key, true, keyVpnNode);
507 decisionProcess(nodeNlri, keyVpnNode);
508 }
509 }
510 }
511 }
512
513 /**
514 * Update localRIB link based on avaliable peer adjacency RIB.
515 *
516 * @param o adjacency-in/VPN adjacency-in
Priyanka Bfc51c952016-03-26 14:30:33 +0530517 * @throws BgpParseException BGP parse exceptions
Shashikanth VH3fe37982015-11-30 11:50:07 +0530518 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530519 public void localRibUpdateLink(Object o) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530520
521 if (o instanceof AdjRibIn) {
522 AdjRibIn adjRib = (AdjRibIn) o;
523 log.debug("Update local RIB link.");
524
525 Set<BgpLinkLSIdentifier> linkKeys = adjRib.linkTree().keySet();
526 for (BgpLinkLSIdentifier key : linkKeys) {
527 PathAttrNlriDetails pathAttrNlri = adjRib.linkTree().get(key);
528 BgpLinkLsNlriVer4 linkNlri = new BgpLinkLsNlriVer4(pathAttrNlri.protocolID().getType(),
529 pathAttrNlri.identifier(), key, null, false);
530 decisionProcess(linkNlri);
531 }
532 }
533
534 if (o instanceof VpnAdjRibIn) {
535 VpnAdjRibIn vpnAdjRib = (VpnAdjRibIn) o;
536 log.debug("Update local RIB VPN link");
537
538 Set<RouteDistinguisher> linkKeysVpn = vpnAdjRib.vpnLinkTree().keySet();
539 Map<BgpLinkLSIdentifier, PathAttrNlriDetails> link;
540 for (RouteDistinguisher keyVpnLink : linkKeysVpn) {
541 link = vpnAdjRib.vpnLinkTree().get(keyVpnLink);
542
543 Set<BgpLinkLSIdentifier> vpnLinkKeys = link.keySet();
544 for (BgpLinkLSIdentifier key : vpnLinkKeys) {
545 PathAttrNlriDetails pathAttrNlri = vpnAdjRib.linkTree().get(key);
546 BgpLinkLsNlriVer4 linkNlri = new BgpLinkLsNlriVer4(pathAttrNlri.protocolID().getType(),
547 pathAttrNlri.identifier(), key, keyVpnLink,
548 true);
549 decisionProcess(linkNlri, keyVpnLink);
550 }
551 }
552 }
553 }
554
555 /**
556 * Update localRIB prefix based on avaliable peer adjacency RIB.
557 *
558 * @param o instance of adjacency-in/VPN adjacency-in
Priyanka Bfc51c952016-03-26 14:30:33 +0530559 * @throws BgpParseException BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530560 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530561 public void localRibUpdatePrefix(Object o) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530562
563 if (o instanceof AdjRibIn) {
564 AdjRibIn adjRib = (AdjRibIn) o;
565 log.debug("Update local RIB prefix.");
566
567 Set<BgpPrefixLSIdentifier> prefixKeys = adjRib.prefixTree().keySet();
568 for (BgpPrefixLSIdentifier key : prefixKeys) {
569 PathAttrNlriDetails pathAttrNlri = adjRib.prefixTree().get(key);
570 BgpPrefixIPv4LSNlriVer4 prefixNlri = new BgpPrefixIPv4LSNlriVer4(
571 pathAttrNlri.identifier(),
572 pathAttrNlri.protocolID().getType(),
573 key, null, false);
574 decisionProcess(prefixNlri);
575 }
576 }
577
578 if (o instanceof VpnAdjRibIn) {
579 VpnAdjRibIn vpnAdjRib = (VpnAdjRibIn) o;
580 log.debug("Update local RIB VPN prefix.");
581
582 Set<RouteDistinguisher> prefixKeysVpn = vpnAdjRib.vpnPrefixTree().keySet();
583 Map<BgpPrefixLSIdentifier, PathAttrNlriDetails> prefix;
584 for (RouteDistinguisher keyVpnPrefix : prefixKeysVpn) {
585 prefix = vpnAdjRib.vpnPrefixTree().get(keyVpnPrefix);
586
587 Set<BgpPrefixLSIdentifier> vpnPrefixKeys = prefix.keySet();
588 for (BgpPrefixLSIdentifier key : vpnPrefixKeys) {
589 PathAttrNlriDetails pathAttrNlri = vpnAdjRib.prefixTree().get(key);
590 BgpPrefixIPv4LSNlriVer4 prefixNlri = new BgpPrefixIPv4LSNlriVer4(pathAttrNlri.identifier(),
591 pathAttrNlri.protocolID()
592 .getType(), key,
593 keyVpnPrefix, true);
594 decisionProcess(prefixNlri, keyVpnPrefix);
595 }
596 }
597 }
598 }
599
600 /**
601 * Update localRIB.
602 *
603 * @param adjRibIn adjacency RIB-in
Priyanka Bfc51c952016-03-26 14:30:33 +0530604 * @throws BgpParseException BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530605 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530606 public void localRibUpdate(AdjRibIn adjRibIn) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530607 log.debug("Update local RIB.");
608
Jonathan Hart51539b82015-10-29 09:53:04 -0700609 localRibUpdateNode(adjRibIn);
610 localRibUpdateLink(adjRibIn);
611 localRibUpdatePrefix(adjRibIn);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530612 }
613
614 /**
615 * Update localRIB.
616 *
617 * @param vpnAdjRibIn VPN adjacency RIB-in
Priyanka Bfc51c952016-03-26 14:30:33 +0530618 * @throws BgpParseException BGP parse exception
Shashikanth VH3fe37982015-11-30 11:50:07 +0530619 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530620 public void localRibUpdate(VpnAdjRibIn vpnAdjRibIn) throws BgpParseException {
Shashikanth VH3fe37982015-11-30 11:50:07 +0530621 log.debug("Update VPN local RIB.");
622
Jonathan Hart51539b82015-10-29 09:53:04 -0700623 localRibUpdateNode(vpnAdjRibIn);
624 localRibUpdateLink(vpnAdjRibIn);
625 localRibUpdatePrefix(vpnAdjRibIn);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530626 }
627
628 @Override
629 public String toString() {
630 return MoreObjects.toStringHelper(getClass()).omitNullValues().add("nodeTree", nodeTree)
631 .add("linkTree", linkTree).add("prefixTree", prefixTree).add("vpnNodeTree", vpnNodeTree)
632 .add("vpnLinkTree", vpnLinkTree).add("vpnPrefixTree", vpnPrefixTree).toString();
633 }
634}