blob: 8a9ea91c62bdba0619016f9494ae32dd9e9d3b90 [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;
30import org.onosproject.bgpio.types.RouteDistinguisher;
31
32import com.google.common.base.MoreObjects;
33
34/**
35 * Implementation of Adj-RIB-In with VPN for each peer.
36 */
37public class VpnAdjRibIn {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053038 private Map<BgpNodeLSIdentifier, PathAttrNlriDetails> nodeTree = new TreeMap<>();
39 private Map<BgpLinkLSIdentifier, PathAttrNlriDetails> linkTree = new TreeMap<>();
40 private Map<BgpPrefixLSIdentifier, PathAttrNlriDetails> prefixTree = new TreeMap<>();
Priyanka B04b639b2015-11-21 15:15:10 +053041
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053042 private Map<RouteDistinguisher, Map<BgpNodeLSIdentifier, PathAttrNlriDetails>> vpnNodeTree
Priyanka B04b639b2015-11-21 15:15:10 +053043 = new TreeMap<>();
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053044 private Map<RouteDistinguisher, Map<BgpLinkLSIdentifier, PathAttrNlriDetails>> vpnLinkTree
Priyanka B04b639b2015-11-21 15:15:10 +053045 = new TreeMap<>();
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053046 private Map<RouteDistinguisher, Map<BgpPrefixLSIdentifier, PathAttrNlriDetails>> vpnPrefixTree
Priyanka B04b639b2015-11-21 15:15:10 +053047 = new TreeMap<>();
48 /**
49 * Returns the adjacency node.
50 *
51 * @return node adjacency RIB node
52 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053053 public Map<BgpNodeLSIdentifier, PathAttrNlriDetails> nodeTree() {
Priyanka B04b639b2015-11-21 15:15:10 +053054 return nodeTree;
55 }
56
57 /**
58 * Returns the adjacency link.
59 *
60 * @return link adjacency RIB node
61 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053062 public Map<BgpLinkLSIdentifier, PathAttrNlriDetails> linkTree() {
Priyanka B04b639b2015-11-21 15:15:10 +053063 return linkTree;
64 }
65
66 /**
67 * Returns the adjacency prefix.
68 *
69 * @return prefix adjacency RIB node
70 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053071 public Map<BgpPrefixLSIdentifier, PathAttrNlriDetails> prefixTree() {
Priyanka B04b639b2015-11-21 15:15:10 +053072 return prefixTree;
73 }
74
75 /**
76 * Returns the adjacency vpnNode.
77 *
78 * @return vpnNode adjacency RIB node
79 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053080 public Map<RouteDistinguisher, Map<BgpNodeLSIdentifier, PathAttrNlriDetails>> vpnNodeTree() {
Priyanka B04b639b2015-11-21 15:15:10 +053081 return vpnNodeTree;
82 }
83
84 /**
85 * Returns the adjacency vpnLink.
86 *
87 * @return vpnLink adjacency RIB node
88 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053089 public Map<RouteDistinguisher, Map<BgpLinkLSIdentifier, PathAttrNlriDetails>> vpnLinkTree() {
Priyanka B04b639b2015-11-21 15:15:10 +053090 return vpnLinkTree;
91 }
92
93 /**
94 * Returns the adjacency vpnPrefix.
95 *
96 * @return vpnPrefix adjacency RIB node
97 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053098 public Map<RouteDistinguisher, Map<BgpPrefixLSIdentifier, PathAttrNlriDetails>> vpnPrefixTree() {
Priyanka B04b639b2015-11-21 15:15:10 +053099 return vpnPrefixTree;
100 }
101
102 /**
103 * Update vpn nlri identifier into the tree if nlri identifier exists in tree otherwise add this to the tree.
104 *
105 * @param nlri NLRI info
106 * @param details has pathattribute , protocolID and identifier
107 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530108 public void add(BgpLSNlri nlri, PathAttrNlriDetails details) {
109 if (nlri instanceof BgpNodeLSNlriVer4) {
110 BgpNodeLSIdentifier nodeLSIdentifier = ((BgpNodeLSNlriVer4) nlri).getLocalNodeDescriptors();
Priyanka B04b639b2015-11-21 15:15:10 +0530111 if (nodeTree.containsKey(nodeLSIdentifier)) {
112 nodeTree.replace(nodeLSIdentifier, details);
113 } else {
114 nodeTree.put(nodeLSIdentifier, details);
115 }
116 } else if (nlri instanceof BgpLinkLsNlriVer4) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530117 BgpLinkLSIdentifier linkLSIdentifier = ((BgpLinkLsNlriVer4) nlri).getLinkIdentifier();
Priyanka B04b639b2015-11-21 15:15:10 +0530118 if (linkTree.containsKey(linkLSIdentifier)) {
119 linkTree.replace(linkLSIdentifier, details);
120 } else {
121 linkTree.put(linkLSIdentifier, details);
122 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530123 } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) {
124 BgpPrefixLSIdentifier prefixIdentifier = ((BgpPrefixIPv4LSNlriVer4) nlri).getPrefixIdentifier();
Priyanka B04b639b2015-11-21 15:15:10 +0530125 if (prefixTree.containsKey(prefixIdentifier)) {
126 prefixTree.replace(prefixIdentifier, details);
127 } else {
128 prefixTree.put(prefixIdentifier, details);
129 }
130 }
131 }
132
133 /**
134 * Update nlri identifier mapped with route distinguisher if it exists in tree otherwise add nlri infomation mapped
135 * to respective route distinguisher in tree.
136 *
137 * @param nlri NLRI info
138 * @param details has pathattribute , protocolID and identifier
139 * @param routeDistinguisher unique for for each vpn
140 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530141 public void addVpn(BgpLSNlri nlri, PathAttrNlriDetails details, RouteDistinguisher routeDistinguisher) {
Priyanka B04b639b2015-11-21 15:15:10 +0530142 add(nlri, details);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530143 if (nlri instanceof BgpNodeLSNlriVer4) {
Priyanka B04b639b2015-11-21 15:15:10 +0530144 if (!vpnNodeTree.containsKey(routeDistinguisher)) {
145 vpnNodeTree.put(routeDistinguisher, nodeTree);
146 }
147 } else if (nlri instanceof BgpLinkLsNlriVer4) {
148 if (!vpnLinkTree.containsKey(routeDistinguisher)) {
149 vpnLinkTree.put(routeDistinguisher, linkTree);
150 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530151 } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) {
Priyanka B04b639b2015-11-21 15:15:10 +0530152 if (!vpnPrefixTree.containsKey(routeDistinguisher)) {
153 vpnPrefixTree.put(routeDistinguisher, prefixTree);
154 }
155 }
156 }
157
158 /**
159 * Removes vpn nlri identifier mapped to route distinguisher if it exists in tree.
160 *
161 * @param nlri NLRI Info
162 * @param routeDistinguisher unique for for each vpn
163 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530164 public void removeVpn(BgpLSNlri nlri, RouteDistinguisher routeDistinguisher) {
165 if (nlri instanceof BgpNodeLSNlriVer4) {
Priyanka B04b639b2015-11-21 15:15:10 +0530166 if (vpnNodeTree.containsKey(routeDistinguisher)) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530167 BgpNodeLSIdentifier nodeLSIdentifier = ((BgpNodeLSNlriVer4) nlri).getLocalNodeDescriptors();
Priyanka B04b639b2015-11-21 15:15:10 +0530168 if (nodeTree.containsKey(nodeLSIdentifier)) {
169 nodeTree.remove(nodeLSIdentifier);
170 }
171 if ((vpnNodeTree.get(routeDistinguisher)).isEmpty()) {
172 vpnNodeTree.remove(routeDistinguisher);
173 }
174 }
175 } else if (nlri instanceof BgpLinkLsNlriVer4) {
176 if (vpnLinkTree.containsKey(routeDistinguisher)) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530177 BgpLinkLSIdentifier linkLSIdentifier = ((BgpLinkLsNlriVer4) nlri).getLinkIdentifier();
Priyanka B04b639b2015-11-21 15:15:10 +0530178 if (linkTree.containsKey(linkLSIdentifier)) {
179 linkTree.remove(linkLSIdentifier);
180 }
181 if ((vpnLinkTree.get(routeDistinguisher)).isEmpty()) {
182 vpnLinkTree.remove(routeDistinguisher);
183 }
184 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530185 } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) {
Priyanka B04b639b2015-11-21 15:15:10 +0530186 if (vpnPrefixTree.containsKey(routeDistinguisher)) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530187 BgpPrefixLSIdentifier prefixIdentifier = ((BgpPrefixIPv4LSNlriVer4) nlri).getPrefixIdentifier();
Priyanka B04b639b2015-11-21 15:15:10 +0530188 if (prefixTree.containsKey(prefixIdentifier)) {
189 prefixTree.remove(prefixIdentifier);
190 }
191 if ((vpnPrefixTree.get(routeDistinguisher)).isEmpty()) {
192 vpnPrefixTree.remove(routeDistinguisher);
193 }
194 }
195 }
196 }
197
198 @Override
199 public String toString() {
200 return MoreObjects.toStringHelper(getClass())
201 .omitNullValues().add("nodeTree", nodeTree)
202 .add("linkTree", linkTree)
203 .add("prefixTree", prefixTree)
204 .add("vpnNodeTree", vpnNodeTree)
205 .add("vpnLinkTree", vpnLinkTree)
206 .add("vpnPrefixTree", vpnPrefixTree)
207 .toString();
208 }
209}