blob: 4dc8feebe8bc85684f44528e27c7b254bbe74200 [file] [log] [blame]
Shashikanth VH6de20d32015-10-09 12:04:13 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Shashikanth VH6de20d32015-10-09 12:04:13 +05303 *
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
Jonathan Hart51539b82015-10-29 09:53:04 -070019import com.google.common.base.MoreObjects;
Shashikanth VHafb2e002016-02-12 14:48:29 +053020import com.google.common.base.Preconditions;
21
Shashikanth VH6de20d32015-10-09 12:04:13 +053022import org.jboss.netty.channel.Channel;
23import org.onlab.packet.IpAddress;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053024import org.onosproject.bgp.controller.BgpController;
Jonathan Hart51539b82015-10-29 09:53:04 -070025import org.onosproject.bgp.controller.BgpLocalRib;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053026import org.onosproject.bgp.controller.BgpPeer;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053027import org.onosproject.bgp.controller.BgpSessionInfo;
Priyanka Bc08e56d2015-11-27 15:28:33 +053028import org.onosproject.bgpio.exceptions.BgpParseException;
Mohammad Shahid30fedc52017-08-09 11:49:40 +053029import org.onosproject.bgpio.protocol.BgpEvpnNlri;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053030import org.onosproject.bgpio.protocol.BgpFactories;
31import org.onosproject.bgpio.protocol.BgpFactory;
Priyanka Bc08e56d2015-11-27 15:28:33 +053032import org.onosproject.bgpio.protocol.BgpLSNlri;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033import org.onosproject.bgpio.protocol.BgpMessage;
Shashikanth VH26fd38a2016-04-26 18:11:37 +053034import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecNlri;
35import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecRouteKey;
Jonathan Hart51539b82015-10-29 09:53:04 -070036import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
Priyanka Bc08e56d2015-11-27 15:28:33 +053037import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
38import org.onosproject.bgpio.protocol.linkstate.BgpPrefixIPv4LSNlriVer4;
Priyanka Bc08e56d2015-11-27 15:28:33 +053039import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails;
Shashikanth VHafb2e002016-02-12 14:48:29 +053040import org.onosproject.bgpio.types.As4Path;
Mohammad Shahid30fedc52017-08-09 11:49:40 +053041import org.onosproject.bgpio.types.AsPath;
Shashikanth VH58260662016-02-13 01:12:02 +053042import org.onosproject.bgpio.types.BgpExtendedCommunity;
Priyanka Bc08e56d2015-11-27 15:28:33 +053043import org.onosproject.bgpio.types.BgpValueType;
Shashikanth VHafb2e002016-02-12 14:48:29 +053044import org.onosproject.bgpio.types.LocalPref;
45import org.onosproject.bgpio.types.Med;
Priyanka Bc08e56d2015-11-27 15:28:33 +053046import org.onosproject.bgpio.types.MpReachNlri;
47import org.onosproject.bgpio.types.MpUnReachNlri;
Shashikanth VHafb2e002016-02-12 14:48:29 +053048import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv;
49import org.onosproject.bgpio.types.Origin;
Shashikanth VH26fd38a2016-04-26 18:11:37 +053050import org.onosproject.bgpio.types.RpdCapabilityTlv;
Mohammad Shahid30fedc52017-08-09 11:49:40 +053051import org.onosproject.bgpio.types.attr.WideCommunity;
Shashikanth VHafb2e002016-02-12 14:48:29 +053052import org.onosproject.bgpio.util.Constants;
Shashikanth VH6de20d32015-10-09 12:04:13 +053053import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
55
Jonathan Hart51539b82015-10-29 09:53:04 -070056import java.net.InetSocketAddress;
57import java.net.SocketAddress;
Shashikanth VH26fd38a2016-04-26 18:11:37 +053058import java.util.ArrayList;
Jonathan Hart51539b82015-10-29 09:53:04 -070059import java.util.Collections;
Shashikanth VH26fd38a2016-04-26 18:11:37 +053060import java.util.LinkedList;
Jonathan Hart51539b82015-10-29 09:53:04 -070061import java.util.List;
62import java.util.ListIterator;
63import java.util.concurrent.RejectedExecutionException;
Shashikanth VH6de20d32015-10-09 12:04:13 +053064
65/**
66 * BGPPeerImpl implements BGPPeer, maintains peer information and store updates in RIB .
67 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053068public class BgpPeerImpl implements BgpPeer {
Shashikanth VH6de20d32015-10-09 12:04:13 +053069
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053070 protected final Logger log = LoggerFactory.getLogger(BgpPeerImpl.class);
Shashikanth VH6de20d32015-10-09 12:04:13 +053071
72 private static final String SHUTDOWN_MSG = "Worker has already been shutdown";
73
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053074 private BgpController bgpController;
Shashikanth VH6de20d32015-10-09 12:04:13 +053075 private Channel channel;
76 protected String channelId;
77 private boolean connected;
78 protected boolean isHandShakeComplete = false;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053079 private BgpSessionInfo sessionInfo;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053080 private BgpPacketStatsImpl pktStats;
Jonathan Hart51539b82015-10-29 09:53:04 -070081 private BgpLocalRib bgplocalRib;
82 private BgpLocalRib bgplocalRibVpn;
Priyanka Bc08e56d2015-11-27 15:28:33 +053083 private AdjRibIn adjRib;
84 private VpnAdjRibIn vpnAdjRib;
Shashikanth VH6de20d32015-10-09 12:04:13 +053085
Shashikanth VH3fe37982015-11-30 11:50:07 +053086 /**
87 * Return the adjacency RIB-IN.
88 *
89 * @return adjRib the adjacency RIB-IN
90 */
91 public AdjRibIn adjacencyRib() {
92 return adjRib;
93 }
94
95 /**
96 * Return the adjacency RIB-IN with VPN.
97 *
98 * @return vpnAdjRib the adjacency RIB-IN with VPN
99 */
100 public VpnAdjRibIn vpnAdjacencyRib() {
101 return vpnAdjRib;
102 }
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530103
Shashikanth VH6de20d32015-10-09 12:04:13 +0530104 @Override
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530105 public BgpSessionInfo sessionInfo() {
106 return sessionInfo;
107 }
108
109 /**
110 * Initialize peer.
111 *
112 *@param bgpController controller instance
113 *@param sessionInfo bgp session info
114 *@param pktStats packet statistics
115 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530116 public BgpPeerImpl(BgpController bgpController, BgpSessionInfo sessionInfo, BgpPacketStatsImpl pktStats) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530117 this.bgpController = bgpController;
118 this.sessionInfo = sessionInfo;
119 this.pktStats = pktStats;
Jonathan Hart51539b82015-10-29 09:53:04 -0700120 this.bgplocalRib = bgpController.bgpLocalRib();
121 this.bgplocalRibVpn = bgpController.bgpLocalRibVpn();
Priyanka Bc08e56d2015-11-27 15:28:33 +0530122 this.adjRib = new AdjRibIn();
123 this.vpnAdjRib = new VpnAdjRibIn();
124 }
125
Shashikanth VHafb2e002016-02-12 14:48:29 +0530126 /**
127 * Check if peer support capability.
128 *
129 * @param type capability type
130 * @param afi address family identifier
131 * @param sAfi subsequent address family identifier
132 * @return true if capability is supported, otherwise false
133 */
134 public final boolean isCapabilitySupported(short type, short afi, byte sAfi) {
135
136 List<BgpValueType> capability = sessionInfo.remoteBgpCapability();
137 ListIterator<BgpValueType> listIterator = capability.listIterator();
138
139 while (listIterator.hasNext()) {
140 BgpValueType tlv = listIterator.next();
141
142 if (tlv.getType() == type) {
143 if (tlv.getType() == MultiProtocolExtnCapabilityTlv.TYPE) {
144 MultiProtocolExtnCapabilityTlv temp = (MultiProtocolExtnCapabilityTlv) tlv;
145 if ((temp.getAfi() == afi) && (temp.getSafi() == sAfi)) {
mohamedrahil00f6f262016-11-24 20:20:41 +0530146 log.debug("Multi prorotcol extension capabality TLV is true");
Shashikanth VHafb2e002016-02-12 14:48:29 +0530147 return true;
mohamedrahil00f6f262016-11-24 20:20:41 +0530148
Shashikanth VHafb2e002016-02-12 14:48:29 +0530149 }
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530150 } else if (tlv.getType() == RpdCapabilityTlv.TYPE) {
151 RpdCapabilityTlv temp = (RpdCapabilityTlv) tlv;
152 if ((temp.getAfi() == afi) && (temp.getSafi() == sAfi)) {
mohamedrahil00f6f262016-11-24 20:20:41 +0530153 log.debug("RPD capabality TLV is true");
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530154 return true;
155 }
Shashikanth VHafb2e002016-02-12 14:48:29 +0530156 }
157 }
158 }
mohamedrahil00f6f262016-11-24 20:20:41 +0530159 log.debug("IS capabality is not supported ");
Shashikanth VHafb2e002016-02-12 14:48:29 +0530160 return false;
161 }
162
163 /**
164 * Send flow specification update message to peer.
165 *
166 * @param operType operation type
Shashikanth VH83f77e42016-05-26 20:34:56 +0530167 * @param routeKey flow rule key
Shashikanth VHafb2e002016-02-12 14:48:29 +0530168 * @param flowSpec flow specification details
Shashikanth VH83f77e42016-05-26 20:34:56 +0530169 * @param wideCommunity for route policy
Shashikanth VHafb2e002016-02-12 14:48:29 +0530170 */
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530171 public final void sendFlowSpecUpdateMessageToPeer(FlowSpecOperation operType, BgpFlowSpecRouteKey routeKey,
172 BgpFlowSpecNlri flowSpec, WideCommunity wideCommunity) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530173
174 List<BgpValueType> attributesList = new LinkedList<>();
175 byte sessionType = sessionInfo.isIbgpSession() ? (byte) 0 : (byte) 1;
Shashikanth VHe38c21e2016-05-26 21:37:25 +0530176 byte sAfi = Constants.SAFI_FLOWSPEC_VALUE;
Shashikanth VHafb2e002016-02-12 14:48:29 +0530177
178 boolean isFsCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
179 Constants.AFI_FLOWSPEC_VALUE,
180 Constants.SAFI_FLOWSPEC_VALUE);
181
182 boolean isVpnFsCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
183 Constants.AFI_FLOWSPEC_VALUE,
184 Constants.VPN_SAFI_FLOWSPEC_VALUE);
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530185
186 boolean isRpdCapabilitySet = isCapabilitySupported(RpdCapabilityTlv.TYPE,
187 Constants.AFI_FLOWSPEC_RPD_VALUE,
188 Constants.SAFI_FLOWSPEC_RPD_VALUE);
189
190 boolean isVpnRpdCapabilitySet = isCapabilitySupported(RpdCapabilityTlv.TYPE,
191 Constants.AFI_FLOWSPEC_RPD_VALUE,
192 Constants.VPN_SAFI_FLOWSPEC_RDP_VALUE);
193
194 if ((!isFsCapabilitySet) && (!isVpnFsCapabilitySet) && (!isRpdCapabilitySet) && (!isVpnRpdCapabilitySet)) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530195 log.debug("Peer do not support BGP flow spec capability", channel.getRemoteAddress());
196 return;
197 }
198
Shashikanth VHe38c21e2016-05-26 21:37:25 +0530199 if (isVpnFsCapabilitySet) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530200 sAfi = Constants.VPN_SAFI_FLOWSPEC_VALUE;
Shashikanth VHe38c21e2016-05-26 21:37:25 +0530201 } else if (isVpnRpdCapabilitySet) {
202 sAfi = Constants.VPN_SAFI_FLOWSPEC_RDP_VALUE;
Shashikanth VHafb2e002016-02-12 14:48:29 +0530203 }
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530204 attributesList.add(new Origin((byte) 0));
Shashikanth VHafb2e002016-02-12 14:48:29 +0530205
206 if (sessionType != 0) {
207 // EBGP
208 if (!bgpController.getConfig().getLargeASCapability()) {
209 List<Short> aspathSet = new ArrayList<>();
210 List<Short> aspathSeq = new ArrayList<>();
211 aspathSeq.add((short) bgpController.getConfig().getAsNumber());
212
213 AsPath asPath = new AsPath(aspathSet, aspathSeq);
214 attributesList.add(asPath);
215 } else {
216 List<Integer> aspathSet = new ArrayList<>();
217 List<Integer> aspathSeq = new ArrayList<>();
218 aspathSeq.add(bgpController.getConfig().getAsNumber());
219
220 As4Path as4Path = new As4Path(aspathSet, aspathSeq);
221 attributesList.add(as4Path);
222 }
223 attributesList.add(new Med(0));
224 } else {
225 attributesList.add(new AsPath());
226 attributesList.add(new Med(0));
227 attributesList.add(new LocalPref(100));
228 }
229
Shashikanth VH58260662016-02-13 01:12:02 +0530230 attributesList.add(new BgpExtendedCommunity(flowSpec.fsActionTlv()));
Shashikanth VHe38c21e2016-05-26 21:37:25 +0530231 if (wideCommunity != null) {
232 attributesList.add(wideCommunity);
233 }
Shashikanth VHafb2e002016-02-12 14:48:29 +0530234
235 if (operType == FlowSpecOperation.ADD) {
236 attributesList.add(new MpReachNlri(flowSpec, Constants.AFI_FLOWSPEC_VALUE, sAfi));
237 } else if (operType == FlowSpecOperation.DELETE) {
238 attributesList.add(new MpUnReachNlri(flowSpec, Constants.AFI_FLOWSPEC_VALUE, sAfi));
239 }
240
241 BgpMessage msg = Controller.getBgpMessageFactory4().updateMessageBuilder()
242 .setBgpPathAttributes(attributesList).build();
243
mohamedrahil00f6f262016-11-24 20:20:41 +0530244 log.debug("Sending flow spec update message to {}", channel.getRemoteAddress());
Shashikanth VHafb2e002016-02-12 14:48:29 +0530245 channel.write(Collections.singletonList(msg));
246 }
247
248 @Override
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530249 public void updateFlowSpec(FlowSpecOperation operType, BgpFlowSpecRouteKey routeKey,
250 BgpFlowSpecNlri flowSpec, WideCommunity wideCommunity) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530251 Preconditions.checkNotNull(operType, "flow specification operation type cannot be null");
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530252 Preconditions.checkNotNull(routeKey, "flow specification prefix cannot be null");
Shashikanth VHafb2e002016-02-12 14:48:29 +0530253 Preconditions.checkNotNull(flowSpec, "flow specification details cannot be null");
254 Preconditions.checkNotNull(flowSpec.fsActionTlv(), "flow specification action cannot be null");
255
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530256 sendFlowSpecUpdateMessageToPeer(operType, routeKey, flowSpec, wideCommunity);
Shashikanth VHafb2e002016-02-12 14:48:29 +0530257 }
Priyanka Bc08e56d2015-11-27 15:28:33 +0530258
259 @Override
Mohammad Shahid30fedc52017-08-09 11:49:40 +0530260 public void updateEvpnNlri(FlowSpecOperation operType, IpAddress nextHop,
261 List<BgpValueType> extcommunity,
262 List<BgpEvpnNlri> evpnNlris) {
263 Preconditions.checkNotNull(operType, "Operation type cannot be null");
264 Preconditions.checkNotNull(evpnNlris, "Evpn nlri cannot be null");
265 sendEvpnUpdateMessageToPeer(operType, nextHop, extcommunity, evpnNlris);
266 }
267
268 private void sendEvpnUpdateMessageToPeer(FlowSpecOperation operType,
269 IpAddress nextHop,
270 List<BgpValueType> extcommunity,
271 List<BgpEvpnNlri> evpnNlris) {
272 List<BgpValueType> attributesList = new LinkedList<>();
273 byte sessionType = sessionInfo.isIbgpSession() ? (byte) 0 : (byte) 1;
274 short afi = Constants.AFI_EVPN_VALUE;
275 byte safi = Constants.SAFI_EVPN_VALUE;
276 boolean isEvpnCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
277 afi, safi);
278 if (!isEvpnCapabilitySet) {
279 log.debug("Peer do not support BGP Evpn capability",
280 channel.getRemoteAddress());
281 return;
282 }
283 attributesList.add(new Origin((byte) 0));
284
285 if (sessionType != 0) {
286 // EBGP
287 if (!bgpController.getConfig().getLargeASCapability()) {
288 List<Short> aspathSet = new ArrayList<>();
289 List<Short> aspathSeq = new ArrayList<>();
290 aspathSeq.add((short) bgpController.getConfig().getAsNumber());
291
292 AsPath asPath = new AsPath(aspathSet, aspathSeq);
293 attributesList.add(asPath);
294 } else {
295 List<Integer> aspathSet = new ArrayList<>();
296 List<Integer> aspathSeq = new ArrayList<>();
297 aspathSeq.add(bgpController.getConfig().getAsNumber());
298
299 As4Path as4Path = new As4Path(aspathSet, aspathSeq);
300 attributesList.add(as4Path);
301 }
302 } else {
303 attributesList.add(new AsPath());
304 }
305
306 if (!extcommunity.isEmpty()) {
307 attributesList.add(new BgpExtendedCommunity(extcommunity));
308 }
309 if (operType == FlowSpecOperation.ADD || operType == FlowSpecOperation.UPDATE) {
310 attributesList
311 .add(new MpReachNlri(evpnNlris, afi, safi, nextHop.getIp4Address()));
312 } else if (operType == FlowSpecOperation.DELETE) {
313 attributesList.add(new MpUnReachNlri(evpnNlris, afi, safi));
314 }
315
316 BgpMessage msg = Controller.getBgpMessageFactory4()
317 .updateMessageBuilder().setBgpPathAttributes(attributesList)
318 .build();
319 channel.write(Collections.singletonList(msg));
320 }
321
322 @Override
Priyanka Bc08e56d2015-11-27 15:28:33 +0530323 public void buildAdjRibIn(List<BgpValueType> pathAttr) throws BgpParseException {
324 ListIterator<BgpValueType> iterator = pathAttr.listIterator();
325 while (iterator.hasNext()) {
326 BgpValueType attr = iterator.next();
327 if (attr instanceof MpReachNlri) {
328 List<BgpLSNlri> nlri = ((MpReachNlri) attr).mpReachNlri();
329 callAdd(this, nlri, pathAttr);
330 }
331 if (attr instanceof MpUnReachNlri) {
332 List<BgpLSNlri> nlri = ((MpUnReachNlri) attr).mpUnReachNlri();
333 callRemove(this, nlri);
334 }
335 }
336 }
337
338 /**
339 * Updates NLRI identifier node in a tree separately based on afi and safi.
340 *
341 * @param peerImpl BGP peer instance
342 * @param nlri MpReachNlri path attribute
343 * @param pathAttr list of BGP path attributes
344 * @throws BgpParseException throws exception
345 */
346 public void callAdd(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri, List<BgpValueType> pathAttr)
347 throws BgpParseException {
348 ListIterator<BgpLSNlri> listIterator = nlri.listIterator();
349 while (listIterator.hasNext()) {
350 BgpLSNlri nlriInfo = listIterator.next();
351 if (nlriInfo instanceof BgpNodeLSNlriVer4) {
352 PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
353 if (!((BgpNodeLSNlriVer4) nlriInfo).isVpnPresent()) {
354 adjRib.add(nlriInfo, details);
Jonathan Hart51539b82015-10-29 09:53:04 -0700355 bgplocalRib.add(sessionInfo(), nlriInfo, details);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530356 } else {
357 vpnAdjRib.addVpn(nlriInfo, details, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700358 bgplocalRibVpn.add(sessionInfo(), nlriInfo, details,
Shashikanth VH3fe37982015-11-30 11:50:07 +0530359 ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530360 }
361 } else if (nlriInfo instanceof BgpLinkLsNlriVer4) {
362 PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
363 if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) {
364 adjRib.add(nlriInfo, details);
Jonathan Hart51539b82015-10-29 09:53:04 -0700365 bgplocalRib.add(sessionInfo(), nlriInfo, details);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530366 } else {
367 vpnAdjRib.addVpn(nlriInfo, details, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700368 bgplocalRibVpn.add(sessionInfo(), nlriInfo, details,
Shashikanth VH3fe37982015-11-30 11:50:07 +0530369 ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530370 }
371 } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) {
372 PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
373 if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) {
374 adjRib.add(nlriInfo, details);
Jonathan Hart51539b82015-10-29 09:53:04 -0700375 bgplocalRib.add(sessionInfo(), nlriInfo, details);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530376 } else {
377 vpnAdjRib.addVpn(nlriInfo, details, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700378 bgplocalRibVpn.add(sessionInfo(), nlriInfo, details,
Shashikanth VH3fe37982015-11-30 11:50:07 +0530379 ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530380 }
381 }
382 }
383 }
384
385 /**
386 * Sets BGP path attribute and NLRI details.
387 *
388 * @param nlriInfo MpReachNlri path attribute
389 * @param pathAttr list of BGP path attributes
390 * @return details object of PathAttrNlriDetails
391 * @throws BgpParseException throw exception
392 */
393 public PathAttrNlriDetails setPathAttrDetails(BgpLSNlri nlriInfo, List<BgpValueType> pathAttr)
394 throws BgpParseException {
395 PathAttrNlriDetails details = new PathAttrNlriDetails();
396 details.setProtocolID(nlriInfo.getProtocolId());
397 details.setIdentifier(nlriInfo.getIdentifier());
398 details.setPathAttribute(pathAttr);
399 return details;
400 }
401
402 /**
403 * Removes NLRI identifier node in a tree separately based on afi and safi.
404 *
405 * @param peerImpl BGP peer instance
406 * @param nlri NLRI information
Priyanka Bfc51c952016-03-26 14:30:33 +0530407 * @throws BgpParseException BGP parse exception
Priyanka Bc08e56d2015-11-27 15:28:33 +0530408 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530409 public void callRemove(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri) throws BgpParseException {
Priyanka Bc08e56d2015-11-27 15:28:33 +0530410 ListIterator<BgpLSNlri> listIterator = nlri.listIterator();
411 while (listIterator.hasNext()) {
412 BgpLSNlri nlriInfo = listIterator.next();
413 if (nlriInfo instanceof BgpNodeLSNlriVer4) {
414 if (!((BgpNodeLSNlriVer4) nlriInfo).isVpnPresent()) {
415 adjRib.remove(nlriInfo);
Jonathan Hart51539b82015-10-29 09:53:04 -0700416 bgplocalRib.delete(nlriInfo);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530417 } else {
418 vpnAdjRib.removeVpn(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700419 bgplocalRibVpn.delete(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530420 }
421 } else if (nlriInfo instanceof BgpLinkLsNlriVer4) {
422 if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) {
423 adjRib.remove(nlriInfo);
Jonathan Hart51539b82015-10-29 09:53:04 -0700424 bgplocalRib.delete(nlriInfo);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530425 } else {
426 vpnAdjRib.removeVpn(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700427 bgplocalRibVpn.delete(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530428 }
429 } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) {
430 if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) {
431 adjRib.remove(nlriInfo);
Jonathan Hart51539b82015-10-29 09:53:04 -0700432 bgplocalRib.delete(nlriInfo);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530433 } else {
434 vpnAdjRib.removeVpn(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700435 bgplocalRibVpn.delete(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530436 }
437 }
438 }
439 }
440
441 /**
442 * Return the adjacency RIB-IN.
443 *
444 * @return adjRib the adjacency RIB-IN
445 */
446 public AdjRibIn adjRib() {
447 return adjRib;
448 }
449
450 /**
451 * Return the adjacency RIB-IN with VPN.
452 *
453 * @return vpnAdjRib the adjacency RIB-IN with VPN
454 */
455 public VpnAdjRibIn vpnAdjRib() {
456 return vpnAdjRib;
Shashikanth VH6de20d32015-10-09 12:04:13 +0530457 }
458
Shashikanth VH3fe37982015-11-30 11:50:07 +0530459 /**
460 * Update localRIB on peer disconnect.
461 *
Priyanka Bfc51c952016-03-26 14:30:33 +0530462 * @throws BgpParseException while updating local RIB
Shashikanth VH3fe37982015-11-30 11:50:07 +0530463 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530464 public void updateLocalRibOnPeerDisconnect() throws BgpParseException {
Jonathan Hart51539b82015-10-29 09:53:04 -0700465 BgpLocalRibImpl localRib = (BgpLocalRibImpl) bgplocalRib;
466 BgpLocalRibImpl localRibVpn = (BgpLocalRibImpl) bgplocalRibVpn;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530467
Jonathan Hart51539b82015-10-29 09:53:04 -0700468 localRib.localRibUpdate(adjacencyRib());
469 localRibVpn.localRibUpdate(vpnAdjacencyRib());
Shashikanth VH3fe37982015-11-30 11:50:07 +0530470 }
471
Shashikanth VH6de20d32015-10-09 12:04:13 +0530472 // ************************
473 // Channel related
474 // ************************
475
476 @Override
477 public final void disconnectPeer() {
478 this.channel.close();
479 }
480
481 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530482 public final void sendMessage(BgpMessage m) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530483 log.debug("Sending message to {}", channel.getRemoteAddress());
484 try {
485 channel.write(Collections.singletonList(m));
486 this.pktStats.addOutPacket();
487 } catch (RejectedExecutionException e) {
488 log.warn(e.getMessage());
489 if (!e.getMessage().contains(SHUTDOWN_MSG)) {
490 throw e;
491 }
492 }
493 }
494
495 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530496 public final void sendMessage(List<BgpMessage> msgs) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530497 try {
498 channel.write(msgs);
499 this.pktStats.addOutPacket(msgs.size());
500 } catch (RejectedExecutionException e) {
501 log.warn(e.getMessage());
502 if (!e.getMessage().contains(SHUTDOWN_MSG)) {
503 throw e;
504 }
505 }
506 }
507
508 @Override
509 public final boolean isConnected() {
510 return this.connected;
511 }
512
513 @Override
514 public final void setConnected(boolean connected) {
515 this.connected = connected;
Jon Hall8c7b06a2017-02-22 13:37:33 -0800516 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530517
518 @Override
519 public final void setChannel(Channel channel) {
520 this.channel = channel;
521 final SocketAddress address = channel.getRemoteAddress();
522 if (address instanceof InetSocketAddress) {
523 final InetSocketAddress inetAddress = (InetSocketAddress) address;
524 final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress());
525 if (ipAddress.isIp4()) {
526 channelId = ipAddress.toString() + ':' + inetAddress.getPort();
527 } else {
528 channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort();
529 }
530 }
Jon Hall8c7b06a2017-02-22 13:37:33 -0800531 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530532
533 @Override
534 public final Channel getChannel() {
535 return this.channel;
Jon Hall8c7b06a2017-02-22 13:37:33 -0800536 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530537
538 @Override
539 public String channelId() {
540 return channelId;
541 }
542
Shashikanth VH6de20d32015-10-09 12:04:13 +0530543 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530544 public BgpFactory factory() {
545 return BgpFactories.getFactory(sessionInfo.remoteBgpVersion());
Shashikanth VH6de20d32015-10-09 12:04:13 +0530546 }
547
548 @Override
549 public boolean isHandshakeComplete() {
550 return isHandShakeComplete;
551 }
552
553 @Override
554 public String toString() {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530555 return MoreObjects.toStringHelper(getClass()).omitNullValues()
556 .add("channel", channelId())
Priyanka Bc08e56d2015-11-27 15:28:33 +0530557 .add("BgpId", sessionInfo().remoteBgpId()).toString();
Shashikanth VH6de20d32015-10-09 12:04:13 +0530558 }
559}