blob: 9cbfbf656ba236bd0480a4e9a333fe4c11e78641 [file] [log] [blame]
Priyanka B04b639b2015-11-21 15:15:10 +05301/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.bgp.controller.impl;
18
19import java.util.Map;
20import java.util.TreeMap;
21
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053022import org.onosproject.bgpio.protocol.BgpLSNlri;
23import org.onosproject.bgpio.protocol.linkstate.BgpLinkLSIdentifier;
Priyanka B04b639b2015-11-21 15:15:10 +053024import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053025import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier;
26import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
27import org.onosproject.bgpio.protocol.linkstate.BgpPrefixIPv4LSNlriVer4;
28import org.onosproject.bgpio.protocol.linkstate.BgpPrefixLSIdentifier;
Priyanka B04b639b2015-11-21 15:15:10 +053029import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails;
30
31import com.google.common.base.MoreObjects;
32
33/**
34 * Implementation of Adj-RIB-In for each peer.
35 */
36public class AdjRibIn {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053037 private Map<BgpNodeLSIdentifier, PathAttrNlriDetails> nodeTree = new TreeMap<>();
38 private Map<BgpLinkLSIdentifier, PathAttrNlriDetails> linkTree = new TreeMap<>();
39 private Map<BgpPrefixLSIdentifier, PathAttrNlriDetails> prefixTree = new TreeMap<>();
Priyanka B04b639b2015-11-21 15:15:10 +053040
41 /**
42 * Returns the adjacency node.
43 *
44 * @return node adjacency RIB node
45 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053046 public Map<BgpNodeLSIdentifier, PathAttrNlriDetails> nodeTree() {
Priyanka B04b639b2015-11-21 15:15:10 +053047 return nodeTree;
48 }
49
50 /**
51 * Returns the adjacency link.
52 *
53 * @return link adjacency RIB node
54 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053055 public Map<BgpLinkLSIdentifier, PathAttrNlriDetails> linkTree() {
Priyanka B04b639b2015-11-21 15:15:10 +053056 return linkTree;
57 }
58
59 /**
60 * Returns the adjacency prefix.
61 *
62 * @return prefix adjacency RIB node
63 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053064 public Map<BgpPrefixLSIdentifier, PathAttrNlriDetails> prefixTree() {
Priyanka B04b639b2015-11-21 15:15:10 +053065 return prefixTree;
66 }
67
68 /**
69 * Update nlri identifier into the tree if nlri identifier exists in tree otherwise add this to the tree.
70 *
71 * @param nlri NLRI Info
72 * @param details has pathattribute , protocolID and identifier
73 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053074 public void add(BgpLSNlri nlri, PathAttrNlriDetails details) {
75 if (nlri instanceof BgpNodeLSNlriVer4) {
76 BgpNodeLSIdentifier nodeLSIdentifier = ((BgpNodeLSNlriVer4) nlri).getLocalNodeDescriptors();
Priyanka B04b639b2015-11-21 15:15:10 +053077 if (nodeTree.containsKey(nodeLSIdentifier)) {
78 nodeTree.replace(nodeLSIdentifier, details);
79 } else {
80 nodeTree.put(nodeLSIdentifier, details);
81 }
82 } else if (nlri instanceof BgpLinkLsNlriVer4) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053083 BgpLinkLSIdentifier linkLSIdentifier = ((BgpLinkLsNlriVer4) nlri).getLinkIdentifier();
Priyanka B04b639b2015-11-21 15:15:10 +053084 if (linkTree.containsKey(linkLSIdentifier)) {
85 linkTree.replace(linkLSIdentifier, details);
86 } else {
87 linkTree.put(linkLSIdentifier, details);
88 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053089 } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) {
90 BgpPrefixLSIdentifier prefixIdentifier = ((BgpPrefixIPv4LSNlriVer4) nlri).getPrefixIdentifier();
Priyanka B04b639b2015-11-21 15:15:10 +053091 if (prefixTree.containsKey(prefixIdentifier)) {
92 prefixTree.replace(prefixIdentifier, details);
93 } else {
94 prefixTree.put(prefixIdentifier, details);
95 }
96 }
97 }
98
99 /**
100 * Removes nlri identifier if it exists in the adjacency tree.
101 *
102 * @param nlri NLRI Info
103 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530104 public void remove(BgpLSNlri nlri) {
105 if (nlri instanceof BgpNodeLSNlriVer4) {
106 BgpNodeLSIdentifier nodeLSIdentifier = ((BgpNodeLSNlriVer4) nlri).getLocalNodeDescriptors();
Priyanka B04b639b2015-11-21 15:15:10 +0530107 if (nodeTree.containsKey(nodeLSIdentifier)) {
108 nodeTree.remove(nodeLSIdentifier);
109 }
110 } else if (nlri instanceof BgpLinkLsNlriVer4) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530111 BgpLinkLSIdentifier linkLSIdentifier = ((BgpLinkLsNlriVer4) nlri).getLinkIdentifier();
Priyanka B04b639b2015-11-21 15:15:10 +0530112 if (linkTree.containsKey(linkLSIdentifier)) {
113 linkTree.remove(linkLSIdentifier);
114 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530115 } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) {
116 BgpPrefixLSIdentifier prefixIdentifier = ((BgpPrefixIPv4LSNlriVer4) nlri).getPrefixIdentifier();
Priyanka B04b639b2015-11-21 15:15:10 +0530117 if (prefixTree.containsKey(prefixIdentifier)) {
118 prefixTree.remove(prefixIdentifier);
119 }
120 }
121 }
122
123 @Override
124 public String toString() {
125 return MoreObjects.toStringHelper(getClass())
126 .omitNullValues()
127 .add("nodeTree", nodeTree)
128 .add("linkTree", linkTree)
129 .add("prefixTree", prefixTree)
130 .toString();
131 }
132}