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 | |
| 31 | import com.google.common.base.MoreObjects; |
| 32 | |
| 33 | /** |
| 34 | * Implementation of Adj-RIB-In for each peer. |
| 35 | */ |
| 36 | public class AdjRibIn { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 37 | private Map<BgpNodeLSIdentifier, PathAttrNlriDetails> nodeTree = new TreeMap<>(); |
| 38 | private Map<BgpLinkLSIdentifier, PathAttrNlriDetails> linkTree = new TreeMap<>(); |
| 39 | private Map<BgpPrefixLSIdentifier, PathAttrNlriDetails> prefixTree = new TreeMap<>(); |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Returns the adjacency node. |
| 43 | * |
| 44 | * @return node adjacency RIB node |
| 45 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 46 | public Map<BgpNodeLSIdentifier, PathAttrNlriDetails> nodeTree() { |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 47 | return nodeTree; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns the adjacency link. |
| 52 | * |
| 53 | * @return link adjacency RIB node |
| 54 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 55 | public Map<BgpLinkLSIdentifier, PathAttrNlriDetails> linkTree() { |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 56 | return linkTree; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns the adjacency prefix. |
| 61 | * |
| 62 | * @return prefix adjacency RIB node |
| 63 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 64 | public Map<BgpPrefixLSIdentifier, PathAttrNlriDetails> prefixTree() { |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 65 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 74 | public void add(BgpLSNlri nlri, PathAttrNlriDetails details) { |
| 75 | if (nlri instanceof BgpNodeLSNlriVer4) { |
| 76 | BgpNodeLSIdentifier nodeLSIdentifier = ((BgpNodeLSNlriVer4) nlri).getLocalNodeDescriptors(); |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 77 | if (nodeTree.containsKey(nodeLSIdentifier)) { |
| 78 | nodeTree.replace(nodeLSIdentifier, details); |
| 79 | } else { |
| 80 | nodeTree.put(nodeLSIdentifier, details); |
| 81 | } |
| 82 | } else if (nlri instanceof BgpLinkLsNlriVer4) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 83 | BgpLinkLSIdentifier linkLSIdentifier = ((BgpLinkLsNlriVer4) nlri).getLinkIdentifier(); |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 84 | if (linkTree.containsKey(linkLSIdentifier)) { |
| 85 | linkTree.replace(linkLSIdentifier, details); |
| 86 | } else { |
| 87 | linkTree.put(linkLSIdentifier, details); |
| 88 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 89 | } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) { |
| 90 | BgpPrefixLSIdentifier prefixIdentifier = ((BgpPrefixIPv4LSNlriVer4) nlri).getPrefixIdentifier(); |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 91 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 104 | public void remove(BgpLSNlri nlri) { |
| 105 | if (nlri instanceof BgpNodeLSNlriVer4) { |
| 106 | BgpNodeLSIdentifier nodeLSIdentifier = ((BgpNodeLSNlriVer4) nlri).getLocalNodeDescriptors(); |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 107 | if (nodeTree.containsKey(nodeLSIdentifier)) { |
| 108 | nodeTree.remove(nodeLSIdentifier); |
| 109 | } |
| 110 | } else if (nlri instanceof BgpLinkLsNlriVer4) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 111 | BgpLinkLSIdentifier linkLSIdentifier = ((BgpLinkLsNlriVer4) nlri).getLinkIdentifier(); |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 112 | if (linkTree.containsKey(linkLSIdentifier)) { |
| 113 | linkTree.remove(linkLSIdentifier); |
| 114 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 115 | } else if (nlri instanceof BgpPrefixIPv4LSNlriVer4) { |
| 116 | BgpPrefixLSIdentifier prefixIdentifier = ((BgpPrefixIPv4LSNlriVer4) nlri).getPrefixIdentifier(); |
Priyanka B | 04b639b | 2015-11-21 15:15:10 +0530 | [diff] [blame] | 117 | 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 | } |