Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.bgp.controller.impl; |
| 18 | |
| 19 | import java.util.Map; |
| 20 | import java.util.TreeMap; |
| 21 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 22 | import org.onosproject.bgpio.protocol.BgpLSNlri; |
| 23 | import org.onosproject.bgpio.protocol.linkstate.BgpLinkLSIdentifier; |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 24 | import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 25 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier; |
| 26 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4; |
| 27 | import org.onosproject.bgpio.protocol.linkstate.BgpPrefixIPv4LSNlriVer4; |
| 28 | import org.onosproject.bgpio.protocol.linkstate.BgpPrefixLSIdentifier; |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 29 | import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails; |
| 30 | import org.onosproject.bgpio.types.RouteDistinguisher; |
| 31 | |
| 32 | import com.google.common.base.MoreObjects; |
| 33 | |
| 34 | /** |
| 35 | * Implementation of Adj-RIB-In with VPN for each peer. |
| 36 | */ |
| 37 | public class VpnAdjRibIn { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 38 | private Map<BgpNodeLSIdentifier, PathAttrNlriDetails> nodeTree = new TreeMap<>(); |
| 39 | private Map<BgpLinkLSIdentifier, PathAttrNlriDetails> linkTree = new TreeMap<>(); |
| 40 | private Map<BgpPrefixLSIdentifier, PathAttrNlriDetails> prefixTree = new TreeMap<>(); |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 41 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 42 | private Map<RouteDistinguisher, Map<BgpNodeLSIdentifier, PathAttrNlriDetails>> vpnNodeTree |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 43 | = new TreeMap<>(); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 44 | private Map<RouteDistinguisher, Map<BgpLinkLSIdentifier, PathAttrNlriDetails>> vpnLinkTree |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 45 | = new TreeMap<>(); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 46 | private Map<RouteDistinguisher, Map<BgpPrefixLSIdentifier, PathAttrNlriDetails>> vpnPrefixTree |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 47 | = new TreeMap<>(); |
| 48 | /** |
| 49 | * Returns the adjacency node. |
| 50 | * |
| 51 | * @return node adjacency RIB node |
| 52 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 53 | public Map<BgpNodeLSIdentifier, PathAttrNlriDetails> nodeTree() { |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 54 | return nodeTree; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the adjacency link. |
| 59 | * |
| 60 | * @return link adjacency RIB node |
| 61 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 62 | public Map<BgpLinkLSIdentifier, PathAttrNlriDetails> linkTree() { |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 63 | return linkTree; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns the adjacency prefix. |
| 68 | * |
| 69 | * @return prefix adjacency RIB node |
| 70 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 71 | public Map<BgpPrefixLSIdentifier, PathAttrNlriDetails> prefixTree() { |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 72 | return prefixTree; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the adjacency vpnNode. |
| 77 | * |
| 78 | * @return vpnNode adjacency RIB node |
| 79 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 80 | public Map<RouteDistinguisher, Map<BgpNodeLSIdentifier, PathAttrNlriDetails>> vpnNodeTree() { |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 81 | return vpnNodeTree; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns the adjacency vpnLink. |
| 86 | * |
| 87 | * @return vpnLink adjacency RIB node |
| 88 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 89 | public Map<RouteDistinguisher, Map<BgpLinkLSIdentifier, PathAttrNlriDetails>> vpnLinkTree() { |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 90 | return vpnLinkTree; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns the adjacency vpnPrefix. |
| 95 | * |
| 96 | * @return vpnPrefix adjacency RIB node |
| 97 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 98 | public Map<RouteDistinguisher, Map<BgpPrefixLSIdentifier, PathAttrNlriDetails>> vpnPrefixTree() { |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 99 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 108 | public void add(BgpLSNlri nlri, PathAttrNlriDetails details) { |
| 109 | if (nlri instanceof BgpNodeLSNlriVer4) { |
| 110 | BgpNodeLSIdentifier nodeLSIdentifier = ((BgpNodeLSNlriVer4) nlri).getLocalNodeDescriptors(); |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 111 | if (nodeTree.containsKey(nodeLSIdentifier)) { |
| 112 | nodeTree.replace(nodeLSIdentifier, details); |
| 113 | } else { |
| 114 | nodeTree.put(nodeLSIdentifier, details); |
| 115 | } |
| 116 | } else if (nlri instanceof BgpLinkLsNlriVer4) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 117 | BgpLinkLSIdentifier linkLSIdentifier = ((BgpLinkLsNlriVer4) nlri).getLinkIdentifier(); |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 118 | if (linkTree.containsKey(linkLSIdentifier)) { |
| 119 | linkTree.replace(linkLSIdentifier, details); |
| 120 | } else { |
| 121 | linkTree.put(linkLSIdentifier, details); |
| 122 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 123 | } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) { |
| 124 | BgpPrefixLSIdentifier prefixIdentifier = ((BgpPrefixIPv4LSNlriVer4) nlri).getPrefixIdentifier(); |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 125 | if (prefixTree.containsKey(prefixIdentifier)) { |
| 126 | prefixTree.replace(prefixIdentifier, details); |
| 127 | } else { |
| 128 | prefixTree.put(prefixIdentifier, details); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
Ray Milkey | c108a6b | 2017-08-23 15:23:50 -0700 | [diff] [blame] | 134 | * Update nlri identifier mapped with route distinguisher if it exists in tree otherwise add nlri information mapped |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 141 | public void addVpn(BgpLSNlri nlri, PathAttrNlriDetails details, RouteDistinguisher routeDistinguisher) { |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 142 | add(nlri, details); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 143 | if (nlri instanceof BgpNodeLSNlriVer4) { |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 144 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 151 | } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) { |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 152 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 164 | public void removeVpn(BgpLSNlri nlri, RouteDistinguisher routeDistinguisher) { |
| 165 | if (nlri instanceof BgpNodeLSNlriVer4) { |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 166 | if (vpnNodeTree.containsKey(routeDistinguisher)) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 167 | BgpNodeLSIdentifier nodeLSIdentifier = ((BgpNodeLSNlriVer4) nlri).getLocalNodeDescriptors(); |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 168 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 177 | BgpLinkLSIdentifier linkLSIdentifier = ((BgpLinkLsNlriVer4) nlri).getLinkIdentifier(); |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 178 | if (linkTree.containsKey(linkLSIdentifier)) { |
| 179 | linkTree.remove(linkLSIdentifier); |
| 180 | } |
| 181 | if ((vpnLinkTree.get(routeDistinguisher)).isEmpty()) { |
| 182 | vpnLinkTree.remove(routeDistinguisher); |
| 183 | } |
| 184 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 185 | } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) { |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 186 | if (vpnPrefixTree.containsKey(routeDistinguisher)) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 187 | BgpPrefixLSIdentifier prefixIdentifier = ((BgpPrefixIPv4LSNlriVer4) nlri).getPrefixIdentifier(); |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 188 | 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 | } |
Ray Milkey | c108a6b | 2017-08-23 15:23:50 -0700 | [diff] [blame] | 209 | } |