Priyanka B
Committed by Gerrit Code Review

Implementation of Adjacency RIB

Change-Id: I80eedba4ccb53fa84028bb3e6e0c91a954413c03
...@@ -16,8 +16,10 @@ ...@@ -16,8 +16,10 @@
16 package org.onosproject.bgp.controller; 16 package org.onosproject.bgp.controller;
17 import java.util.List; 17 import java.util.List;
18 import org.jboss.netty.channel.Channel; 18 import org.jboss.netty.channel.Channel;
19 +import org.onosproject.bgpio.exceptions.BgpParseException;
19 import org.onosproject.bgpio.protocol.BgpFactory; 20 import org.onosproject.bgpio.protocol.BgpFactory;
20 import org.onosproject.bgpio.protocol.BgpMessage; 21 import org.onosproject.bgpio.protocol.BgpMessage;
22 +import org.onosproject.bgpio.types.BgpValueType;
21 23
22 /** 24 /**
23 * Represents the peer side of an BGP peer. 25 * Represents the peer side of an BGP peer.
...@@ -95,6 +97,14 @@ public interface BgpPeer { ...@@ -95,6 +97,14 @@ public interface BgpPeer {
95 String channelId(); 97 String channelId();
96 98
97 /** 99 /**
100 + * Maintaining Adj-RIB-In separately for each peer.
101 + *
102 + * @param pathAttr list of Bgp path attributes
103 + * @throws BgpParseException while building Adj-Rib-In
104 + */
105 + void buildAdjRibIn(List<BgpValueType> pathAttr) throws BgpParseException;
106 +
107 + /**
98 * Return the BGP session info. 108 * Return the BGP session info.
99 * 109 *
100 * @return sessionInfo bgp session info 110 * @return sessionInfo bgp session info
......
...@@ -20,6 +20,7 @@ import java.net.InetSocketAddress; ...@@ -20,6 +20,7 @@ import java.net.InetSocketAddress;
20 import java.net.SocketAddress; 20 import java.net.SocketAddress;
21 import java.util.Collections; 21 import java.util.Collections;
22 import java.util.List; 22 import java.util.List;
23 +import java.util.ListIterator;
23 import java.util.concurrent.RejectedExecutionException; 24 import java.util.concurrent.RejectedExecutionException;
24 25
25 import org.jboss.netty.channel.Channel; 26 import org.jboss.netty.channel.Channel;
...@@ -27,9 +28,18 @@ import org.onlab.packet.IpAddress; ...@@ -27,9 +28,18 @@ import org.onlab.packet.IpAddress;
27 import org.onosproject.bgp.controller.BgpController; 28 import org.onosproject.bgp.controller.BgpController;
28 import org.onosproject.bgp.controller.BgpPeer; 29 import org.onosproject.bgp.controller.BgpPeer;
29 import org.onosproject.bgp.controller.BgpSessionInfo; 30 import org.onosproject.bgp.controller.BgpSessionInfo;
31 +import org.onosproject.bgpio.exceptions.BgpParseException;
30 import org.onosproject.bgpio.protocol.BgpFactories; 32 import org.onosproject.bgpio.protocol.BgpFactories;
31 import org.onosproject.bgpio.protocol.BgpFactory; 33 import org.onosproject.bgpio.protocol.BgpFactory;
34 +import org.onosproject.bgpio.protocol.BgpLSNlri;
32 import org.onosproject.bgpio.protocol.BgpMessage; 35 import org.onosproject.bgpio.protocol.BgpMessage;
36 +import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
37 +import org.onosproject.bgpio.protocol.linkstate.BgpPrefixIPv4LSNlriVer4;
38 +import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
39 +import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails;
40 +import org.onosproject.bgpio.types.BgpValueType;
41 +import org.onosproject.bgpio.types.MpReachNlri;
42 +import org.onosproject.bgpio.types.MpUnReachNlri;
33 import org.slf4j.Logger; 43 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory; 44 import org.slf4j.LoggerFactory;
35 45
...@@ -51,6 +61,8 @@ public class BgpPeerImpl implements BgpPeer { ...@@ -51,6 +61,8 @@ public class BgpPeerImpl implements BgpPeer {
51 protected boolean isHandShakeComplete = false; 61 protected boolean isHandShakeComplete = false;
52 private BgpSessionInfo sessionInfo; 62 private BgpSessionInfo sessionInfo;
53 private BgpPacketStatsImpl pktStats; 63 private BgpPacketStatsImpl pktStats;
64 + private AdjRibIn adjRib;
65 + private VpnAdjRibIn vpnAdjRib;
54 66
55 67
56 @Override 68 @Override
...@@ -69,6 +81,130 @@ public class BgpPeerImpl implements BgpPeer { ...@@ -69,6 +81,130 @@ public class BgpPeerImpl implements BgpPeer {
69 this.bgpController = bgpController; 81 this.bgpController = bgpController;
70 this.sessionInfo = sessionInfo; 82 this.sessionInfo = sessionInfo;
71 this.pktStats = pktStats; 83 this.pktStats = pktStats;
84 + this.adjRib = new AdjRibIn();
85 + this.vpnAdjRib = new VpnAdjRibIn();
86 + }
87 +
88 +
89 + @Override
90 + public void buildAdjRibIn(List<BgpValueType> pathAttr) throws BgpParseException {
91 + ListIterator<BgpValueType> iterator = pathAttr.listIterator();
92 + while (iterator.hasNext()) {
93 + BgpValueType attr = iterator.next();
94 + if (attr instanceof MpReachNlri) {
95 + List<BgpLSNlri> nlri = ((MpReachNlri) attr).mpReachNlri();
96 + callAdd(this, nlri, pathAttr);
97 + }
98 + if (attr instanceof MpUnReachNlri) {
99 + List<BgpLSNlri> nlri = ((MpUnReachNlri) attr).mpUnReachNlri();
100 + callRemove(this, nlri);
101 + }
102 + }
103 + }
104 +
105 + /**
106 + * Updates NLRI identifier node in a tree separately based on afi and safi.
107 + *
108 + * @param peerImpl BGP peer instance
109 + * @param nlri MpReachNlri path attribute
110 + * @param pathAttr list of BGP path attributes
111 + * @throws BgpParseException throws exception
112 + */
113 + public void callAdd(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri, List<BgpValueType> pathAttr)
114 + throws BgpParseException {
115 + ListIterator<BgpLSNlri> listIterator = nlri.listIterator();
116 + while (listIterator.hasNext()) {
117 + BgpLSNlri nlriInfo = listIterator.next();
118 + if (nlriInfo instanceof BgpNodeLSNlriVer4) {
119 + PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
120 + if (!((BgpNodeLSNlriVer4) nlriInfo).isVpnPresent()) {
121 + adjRib.add(nlriInfo, details);
122 + } else {
123 + vpnAdjRib.addVpn(nlriInfo, details, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
124 + }
125 + } else if (nlriInfo instanceof BgpLinkLsNlriVer4) {
126 + PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
127 + if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) {
128 + adjRib.add(nlriInfo, details);
129 + } else {
130 + vpnAdjRib.addVpn(nlriInfo, details, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
131 + }
132 + } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) {
133 + PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
134 + if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) {
135 + adjRib.add(nlriInfo, details);
136 + } else {
137 + vpnAdjRib.addVpn(nlriInfo, details, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
138 + }
139 + }
140 + }
141 + }
142 +
143 + /**
144 + * Sets BGP path attribute and NLRI details.
145 + *
146 + * @param nlriInfo MpReachNlri path attribute
147 + * @param pathAttr list of BGP path attributes
148 + * @return details object of PathAttrNlriDetails
149 + * @throws BgpParseException throw exception
150 + */
151 + public PathAttrNlriDetails setPathAttrDetails(BgpLSNlri nlriInfo, List<BgpValueType> pathAttr)
152 + throws BgpParseException {
153 + PathAttrNlriDetails details = new PathAttrNlriDetails();
154 + details.setProtocolID(nlriInfo.getProtocolId());
155 + details.setIdentifier(nlriInfo.getIdentifier());
156 + details.setPathAttribute(pathAttr);
157 + return details;
158 + }
159 +
160 + /**
161 + * Removes NLRI identifier node in a tree separately based on afi and safi.
162 + *
163 + * @param peerImpl BGP peer instance
164 + * @param nlri NLRI information
165 + */
166 + public void callRemove(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri) {
167 + ListIterator<BgpLSNlri> listIterator = nlri.listIterator();
168 + while (listIterator.hasNext()) {
169 + BgpLSNlri nlriInfo = listIterator.next();
170 + if (nlriInfo instanceof BgpNodeLSNlriVer4) {
171 + if (!((BgpNodeLSNlriVer4) nlriInfo).isVpnPresent()) {
172 + adjRib.remove(nlriInfo);
173 + } else {
174 + vpnAdjRib.removeVpn(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
175 + }
176 + } else if (nlriInfo instanceof BgpLinkLsNlriVer4) {
177 + if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) {
178 + adjRib.remove(nlriInfo);
179 + } else {
180 + vpnAdjRib.removeVpn(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
181 + }
182 + } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) {
183 + if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) {
184 + adjRib.remove(nlriInfo);
185 + } else {
186 + vpnAdjRib.removeVpn(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
187 + }
188 + }
189 + }
190 + }
191 +
192 + /**
193 + * Return the adjacency RIB-IN.
194 + *
195 + * @return adjRib the adjacency RIB-IN
196 + */
197 + public AdjRibIn adjRib() {
198 + return adjRib;
199 + }
200 +
201 + /**
202 + * Return the adjacency RIB-IN with VPN.
203 + *
204 + * @return vpnAdjRib the adjacency RIB-IN with VPN
205 + */
206 + public VpnAdjRibIn vpnAdjRib() {
207 + return vpnAdjRib;
72 } 208 }
73 209
74 // ************************ 210 // ************************
...@@ -156,6 +292,6 @@ public class BgpPeerImpl implements BgpPeer { ...@@ -156,6 +292,6 @@ public class BgpPeerImpl implements BgpPeer {
156 public String toString() { 292 public String toString() {
157 return MoreObjects.toStringHelper(getClass()).omitNullValues() 293 return MoreObjects.toStringHelper(getClass()).omitNullValues()
158 .add("channel", channelId()) 294 .add("channel", channelId())
159 - .add("bgpId", sessionInfo().remoteBgpId()).toString(); 295 + .add("BgpId", sessionInfo().remoteBgpId()).toString();
160 } 296 }
161 } 297 }
......