blob: 0444d69c82c5325fdfc1e0accaaea0bbf03482c4 [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
22import org.onosproject.bgpio.protocol.BGPLSNlri;
23import org.onosproject.bgpio.protocol.linkstate.BGPLinkLSIdentifier;
24import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
25import 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;
29import 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 {
37 private Map<BGPNodeLSIdentifier, PathAttrNlriDetails> nodeTree = new TreeMap<>();
38 private Map<BGPLinkLSIdentifier, PathAttrNlriDetails> linkTree = new TreeMap<>();
39 private Map<BGPPrefixLSIdentifier, PathAttrNlriDetails> prefixTree = new TreeMap<>();
40
41 /**
42 * Returns the adjacency node.
43 *
44 * @return node adjacency RIB node
45 */
46 public Map<BGPNodeLSIdentifier, PathAttrNlriDetails> nodeTree() {
47 return nodeTree;
48 }
49
50 /**
51 * Returns the adjacency link.
52 *
53 * @return link adjacency RIB node
54 */
55 public Map<BGPLinkLSIdentifier, PathAttrNlriDetails> linkTree() {
56 return linkTree;
57 }
58
59 /**
60 * Returns the adjacency prefix.
61 *
62 * @return prefix adjacency RIB node
63 */
64 public Map<BGPPrefixLSIdentifier, PathAttrNlriDetails> prefixTree() {
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 */
74 public void add(BGPLSNlri nlri, PathAttrNlriDetails details) {
75 if (nlri instanceof BGPNodeLSNlriVer4) {
76 BGPNodeLSIdentifier nodeLSIdentifier = ((BGPNodeLSNlriVer4) nlri).getLocalNodeDescriptors();
77 if (nodeTree.containsKey(nodeLSIdentifier)) {
78 nodeTree.replace(nodeLSIdentifier, details);
79 } else {
80 nodeTree.put(nodeLSIdentifier, details);
81 }
82 } else if (nlri instanceof BgpLinkLsNlriVer4) {
83 BGPLinkLSIdentifier linkLSIdentifier = ((BgpLinkLsNlriVer4) nlri).getLinkIdentifier();
84 if (linkTree.containsKey(linkLSIdentifier)) {
85 linkTree.replace(linkLSIdentifier, details);
86 } else {
87 linkTree.put(linkLSIdentifier, details);
88 }
89 } else if (nlri instanceof BGPPrefixIPv4LSNlriVer4) {
90 BGPPrefixLSIdentifier prefixIdentifier = ((BGPPrefixIPv4LSNlriVer4) nlri).getPrefixIdentifier();
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 */
104 public void remove(BGPLSNlri nlri) {
105 if (nlri instanceof BGPNodeLSNlriVer4) {
106 BGPNodeLSIdentifier nodeLSIdentifier = ((BGPNodeLSNlriVer4) nlri).getLocalNodeDescriptors();
107 if (nodeTree.containsKey(nodeLSIdentifier)) {
108 nodeTree.remove(nodeLSIdentifier);
109 }
110 } else if (nlri instanceof BgpLinkLsNlriVer4) {
111 BGPLinkLSIdentifier linkLSIdentifier = ((BgpLinkLsNlriVer4) nlri).getLinkIdentifier();
112 if (linkTree.containsKey(linkLSIdentifier)) {
113 linkTree.remove(linkLSIdentifier);
114 }
115 } else if (nlri instanceof BGPPrefixIPv4LSNlriVer4) {
116 BGPPrefixLSIdentifier prefixIdentifier = ((BGPPrefixIPv4LSNlriVer4) nlri).getPrefixIdentifier();
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}