blob: 44c3bffeeaeb4e2c916d4e77026f30a6153c7bc8 [file] [log] [blame]
Shashikanth VH6de20d32015-10-09 12:04:13 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053029import org.onosproject.bgpio.protocol.BgpFactories;
30import org.onosproject.bgpio.protocol.BgpFactory;
Priyanka Bc08e56d2015-11-27 15:28:33 +053031import org.onosproject.bgpio.protocol.BgpLSNlri;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053032import org.onosproject.bgpio.protocol.BgpMessage;
Shashikanth VH26fd38a2016-04-26 18:11:37 +053033import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecNlri;
34import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecRouteKey;
Jonathan Hart51539b82015-10-29 09:53:04 -070035import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
Priyanka Bc08e56d2015-11-27 15:28:33 +053036import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
37import org.onosproject.bgpio.protocol.linkstate.BgpPrefixIPv4LSNlriVer4;
Priyanka Bc08e56d2015-11-27 15:28:33 +053038import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails;
Shashikanth VHafb2e002016-02-12 14:48:29 +053039import org.onosproject.bgpio.types.AsPath;
40import org.onosproject.bgpio.types.As4Path;
Shashikanth VH58260662016-02-13 01:12:02 +053041import org.onosproject.bgpio.types.BgpExtendedCommunity;
Priyanka Bc08e56d2015-11-27 15:28:33 +053042import org.onosproject.bgpio.types.BgpValueType;
Shashikanth VHafb2e002016-02-12 14:48:29 +053043import org.onosproject.bgpio.types.LocalPref;
44import org.onosproject.bgpio.types.Med;
Priyanka Bc08e56d2015-11-27 15:28:33 +053045import org.onosproject.bgpio.types.MpReachNlri;
46import org.onosproject.bgpio.types.MpUnReachNlri;
Shashikanth VHafb2e002016-02-12 14:48:29 +053047import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv;
48import org.onosproject.bgpio.types.Origin;
Shashikanth VH26fd38a2016-04-26 18:11:37 +053049import org.onosproject.bgpio.types.attr.WideCommunity;
50import org.onosproject.bgpio.types.RpdCapabilityTlv;
Shashikanth VHafb2e002016-02-12 14:48:29 +053051import org.onosproject.bgpio.util.Constants;
Shashikanth VH6de20d32015-10-09 12:04:13 +053052import org.slf4j.Logger;
53import org.slf4j.LoggerFactory;
54
Jonathan Hart51539b82015-10-29 09:53:04 -070055import java.net.InetSocketAddress;
56import java.net.SocketAddress;
Shashikanth VH26fd38a2016-04-26 18:11:37 +053057import java.util.ArrayList;
Jonathan Hart51539b82015-10-29 09:53:04 -070058import java.util.Collections;
Shashikanth VH26fd38a2016-04-26 18:11:37 +053059import java.util.LinkedList;
Jonathan Hart51539b82015-10-29 09:53:04 -070060import java.util.List;
61import java.util.ListIterator;
62import java.util.concurrent.RejectedExecutionException;
Shashikanth VH6de20d32015-10-09 12:04:13 +053063
64/**
65 * BGPPeerImpl implements BGPPeer, maintains peer information and store updates in RIB .
66 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053067public class BgpPeerImpl implements BgpPeer {
Shashikanth VH6de20d32015-10-09 12:04:13 +053068
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053069 protected final Logger log = LoggerFactory.getLogger(BgpPeerImpl.class);
Shashikanth VH6de20d32015-10-09 12:04:13 +053070
71 private static final String SHUTDOWN_MSG = "Worker has already been shutdown";
72
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053073 private BgpController bgpController;
Shashikanth VH6de20d32015-10-09 12:04:13 +053074 private Channel channel;
75 protected String channelId;
76 private boolean connected;
77 protected boolean isHandShakeComplete = false;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053078 private BgpSessionInfo sessionInfo;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053079 private BgpPacketStatsImpl pktStats;
Jonathan Hart51539b82015-10-29 09:53:04 -070080 private BgpLocalRib bgplocalRib;
81 private BgpLocalRib bgplocalRibVpn;
Priyanka Bc08e56d2015-11-27 15:28:33 +053082 private AdjRibIn adjRib;
83 private VpnAdjRibIn vpnAdjRib;
Shashikanth VH6de20d32015-10-09 12:04:13 +053084
Shashikanth VH3fe37982015-11-30 11:50:07 +053085 /**
86 * Return the adjacency RIB-IN.
87 *
88 * @return adjRib the adjacency RIB-IN
89 */
90 public AdjRibIn adjacencyRib() {
91 return adjRib;
92 }
93
94 /**
95 * Return the adjacency RIB-IN with VPN.
96 *
97 * @return vpnAdjRib the adjacency RIB-IN with VPN
98 */
99 public VpnAdjRibIn vpnAdjacencyRib() {
100 return vpnAdjRib;
101 }
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530102
Shashikanth VH6de20d32015-10-09 12:04:13 +0530103 @Override
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530104 public BgpSessionInfo sessionInfo() {
105 return sessionInfo;
106 }
107
108 /**
109 * Initialize peer.
110 *
111 *@param bgpController controller instance
112 *@param sessionInfo bgp session info
113 *@param pktStats packet statistics
114 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530115 public BgpPeerImpl(BgpController bgpController, BgpSessionInfo sessionInfo, BgpPacketStatsImpl pktStats) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530116 this.bgpController = bgpController;
117 this.sessionInfo = sessionInfo;
118 this.pktStats = pktStats;
Jonathan Hart51539b82015-10-29 09:53:04 -0700119 this.bgplocalRib = bgpController.bgpLocalRib();
120 this.bgplocalRibVpn = bgpController.bgpLocalRibVpn();
Priyanka Bc08e56d2015-11-27 15:28:33 +0530121 this.adjRib = new AdjRibIn();
122 this.vpnAdjRib = new VpnAdjRibIn();
123 }
124
Shashikanth VHafb2e002016-02-12 14:48:29 +0530125 /**
126 * Check if peer support capability.
127 *
128 * @param type capability type
129 * @param afi address family identifier
130 * @param sAfi subsequent address family identifier
131 * @return true if capability is supported, otherwise false
132 */
133 public final boolean isCapabilitySupported(short type, short afi, byte sAfi) {
134
135 List<BgpValueType> capability = sessionInfo.remoteBgpCapability();
136 ListIterator<BgpValueType> listIterator = capability.listIterator();
137
138 while (listIterator.hasNext()) {
139 BgpValueType tlv = listIterator.next();
140
141 if (tlv.getType() == type) {
142 if (tlv.getType() == MultiProtocolExtnCapabilityTlv.TYPE) {
143 MultiProtocolExtnCapabilityTlv temp = (MultiProtocolExtnCapabilityTlv) tlv;
144 if ((temp.getAfi() == afi) && (temp.getSafi() == sAfi)) {
mohamedrahil00f6f262016-11-24 20:20:41 +0530145 log.debug("Multi prorotcol extension capabality TLV is true");
Shashikanth VHafb2e002016-02-12 14:48:29 +0530146 return true;
mohamedrahil00f6f262016-11-24 20:20:41 +0530147
Shashikanth VHafb2e002016-02-12 14:48:29 +0530148 }
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530149 } else if (tlv.getType() == RpdCapabilityTlv.TYPE) {
150 RpdCapabilityTlv temp = (RpdCapabilityTlv) tlv;
151 if ((temp.getAfi() == afi) && (temp.getSafi() == sAfi)) {
mohamedrahil00f6f262016-11-24 20:20:41 +0530152 log.debug("RPD capabality TLV is true");
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530153 return true;
154 }
Shashikanth VHafb2e002016-02-12 14:48:29 +0530155 }
156 }
157 }
mohamedrahil00f6f262016-11-24 20:20:41 +0530158 log.debug("IS capabality is not supported ");
Shashikanth VHafb2e002016-02-12 14:48:29 +0530159 return false;
160 }
161
162 /**
163 * Send flow specification update message to peer.
164 *
165 * @param operType operation type
Shashikanth VH83f77e42016-05-26 20:34:56 +0530166 * @param routeKey flow rule key
Shashikanth VHafb2e002016-02-12 14:48:29 +0530167 * @param flowSpec flow specification details
Shashikanth VH83f77e42016-05-26 20:34:56 +0530168 * @param wideCommunity for route policy
Shashikanth VHafb2e002016-02-12 14:48:29 +0530169 */
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530170 public final void sendFlowSpecUpdateMessageToPeer(FlowSpecOperation operType, BgpFlowSpecRouteKey routeKey,
171 BgpFlowSpecNlri flowSpec, WideCommunity wideCommunity) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530172
173 List<BgpValueType> attributesList = new LinkedList<>();
174 byte sessionType = sessionInfo.isIbgpSession() ? (byte) 0 : (byte) 1;
Shashikanth VHe38c21e2016-05-26 21:37:25 +0530175 byte sAfi = Constants.SAFI_FLOWSPEC_VALUE;
Shashikanth VHafb2e002016-02-12 14:48:29 +0530176
177 boolean isFsCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
178 Constants.AFI_FLOWSPEC_VALUE,
179 Constants.SAFI_FLOWSPEC_VALUE);
180
181 boolean isVpnFsCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
182 Constants.AFI_FLOWSPEC_VALUE,
183 Constants.VPN_SAFI_FLOWSPEC_VALUE);
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530184
185 boolean isRpdCapabilitySet = isCapabilitySupported(RpdCapabilityTlv.TYPE,
186 Constants.AFI_FLOWSPEC_RPD_VALUE,
187 Constants.SAFI_FLOWSPEC_RPD_VALUE);
188
189 boolean isVpnRpdCapabilitySet = isCapabilitySupported(RpdCapabilityTlv.TYPE,
190 Constants.AFI_FLOWSPEC_RPD_VALUE,
191 Constants.VPN_SAFI_FLOWSPEC_RDP_VALUE);
192
193 if ((!isFsCapabilitySet) && (!isVpnFsCapabilitySet) && (!isRpdCapabilitySet) && (!isVpnRpdCapabilitySet)) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530194 log.debug("Peer do not support BGP flow spec capability", channel.getRemoteAddress());
195 return;
196 }
197
Shashikanth VHe38c21e2016-05-26 21:37:25 +0530198 if (isVpnFsCapabilitySet) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530199 sAfi = Constants.VPN_SAFI_FLOWSPEC_VALUE;
Shashikanth VHe38c21e2016-05-26 21:37:25 +0530200 } else if (isVpnRpdCapabilitySet) {
201 sAfi = Constants.VPN_SAFI_FLOWSPEC_RDP_VALUE;
Shashikanth VHafb2e002016-02-12 14:48:29 +0530202 }
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530203 attributesList.add(new Origin((byte) 0));
Shashikanth VHafb2e002016-02-12 14:48:29 +0530204
205 if (sessionType != 0) {
206 // EBGP
207 if (!bgpController.getConfig().getLargeASCapability()) {
208 List<Short> aspathSet = new ArrayList<>();
209 List<Short> aspathSeq = new ArrayList<>();
210 aspathSeq.add((short) bgpController.getConfig().getAsNumber());
211
212 AsPath asPath = new AsPath(aspathSet, aspathSeq);
213 attributesList.add(asPath);
214 } else {
215 List<Integer> aspathSet = new ArrayList<>();
216 List<Integer> aspathSeq = new ArrayList<>();
217 aspathSeq.add(bgpController.getConfig().getAsNumber());
218
219 As4Path as4Path = new As4Path(aspathSet, aspathSeq);
220 attributesList.add(as4Path);
221 }
222 attributesList.add(new Med(0));
223 } else {
224 attributesList.add(new AsPath());
225 attributesList.add(new Med(0));
226 attributesList.add(new LocalPref(100));
227 }
228
Shashikanth VH58260662016-02-13 01:12:02 +0530229 attributesList.add(new BgpExtendedCommunity(flowSpec.fsActionTlv()));
Shashikanth VHe38c21e2016-05-26 21:37:25 +0530230 if (wideCommunity != null) {
231 attributesList.add(wideCommunity);
232 }
Shashikanth VHafb2e002016-02-12 14:48:29 +0530233
234 if (operType == FlowSpecOperation.ADD) {
235 attributesList.add(new MpReachNlri(flowSpec, Constants.AFI_FLOWSPEC_VALUE, sAfi));
236 } else if (operType == FlowSpecOperation.DELETE) {
237 attributesList.add(new MpUnReachNlri(flowSpec, Constants.AFI_FLOWSPEC_VALUE, sAfi));
238 }
239
240 BgpMessage msg = Controller.getBgpMessageFactory4().updateMessageBuilder()
241 .setBgpPathAttributes(attributesList).build();
242
mohamedrahil00f6f262016-11-24 20:20:41 +0530243 log.debug("Sending flow spec update message to {}", channel.getRemoteAddress());
Shashikanth VHafb2e002016-02-12 14:48:29 +0530244 channel.write(Collections.singletonList(msg));
245 }
246
247 @Override
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530248 public void updateFlowSpec(FlowSpecOperation operType, BgpFlowSpecRouteKey routeKey,
249 BgpFlowSpecNlri flowSpec, WideCommunity wideCommunity) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530250 Preconditions.checkNotNull(operType, "flow specification operation type cannot be null");
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530251 Preconditions.checkNotNull(routeKey, "flow specification prefix cannot be null");
Shashikanth VHafb2e002016-02-12 14:48:29 +0530252 Preconditions.checkNotNull(flowSpec, "flow specification details cannot be null");
253 Preconditions.checkNotNull(flowSpec.fsActionTlv(), "flow specification action cannot be null");
254
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530255 sendFlowSpecUpdateMessageToPeer(operType, routeKey, flowSpec, wideCommunity);
Shashikanth VHafb2e002016-02-12 14:48:29 +0530256 }
Priyanka Bc08e56d2015-11-27 15:28:33 +0530257
258 @Override
259 public void buildAdjRibIn(List<BgpValueType> pathAttr) throws BgpParseException {
260 ListIterator<BgpValueType> iterator = pathAttr.listIterator();
261 while (iterator.hasNext()) {
262 BgpValueType attr = iterator.next();
263 if (attr instanceof MpReachNlri) {
264 List<BgpLSNlri> nlri = ((MpReachNlri) attr).mpReachNlri();
265 callAdd(this, nlri, pathAttr);
266 }
267 if (attr instanceof MpUnReachNlri) {
268 List<BgpLSNlri> nlri = ((MpUnReachNlri) attr).mpUnReachNlri();
269 callRemove(this, nlri);
270 }
271 }
272 }
273
274 /**
275 * Updates NLRI identifier node in a tree separately based on afi and safi.
276 *
277 * @param peerImpl BGP peer instance
278 * @param nlri MpReachNlri path attribute
279 * @param pathAttr list of BGP path attributes
280 * @throws BgpParseException throws exception
281 */
282 public void callAdd(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri, List<BgpValueType> pathAttr)
283 throws BgpParseException {
284 ListIterator<BgpLSNlri> listIterator = nlri.listIterator();
285 while (listIterator.hasNext()) {
286 BgpLSNlri nlriInfo = listIterator.next();
287 if (nlriInfo instanceof BgpNodeLSNlriVer4) {
288 PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
289 if (!((BgpNodeLSNlriVer4) nlriInfo).isVpnPresent()) {
290 adjRib.add(nlriInfo, details);
Jonathan Hart51539b82015-10-29 09:53:04 -0700291 bgplocalRib.add(sessionInfo(), nlriInfo, details);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530292 } else {
293 vpnAdjRib.addVpn(nlriInfo, details, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700294 bgplocalRibVpn.add(sessionInfo(), nlriInfo, details,
Shashikanth VH3fe37982015-11-30 11:50:07 +0530295 ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530296 }
297 } else if (nlriInfo instanceof BgpLinkLsNlriVer4) {
298 PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
299 if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) {
300 adjRib.add(nlriInfo, details);
Jonathan Hart51539b82015-10-29 09:53:04 -0700301 bgplocalRib.add(sessionInfo(), nlriInfo, details);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530302 } else {
303 vpnAdjRib.addVpn(nlriInfo, details, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700304 bgplocalRibVpn.add(sessionInfo(), nlriInfo, details,
Shashikanth VH3fe37982015-11-30 11:50:07 +0530305 ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530306 }
307 } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) {
308 PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
309 if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) {
310 adjRib.add(nlriInfo, details);
Jonathan Hart51539b82015-10-29 09:53:04 -0700311 bgplocalRib.add(sessionInfo(), nlriInfo, details);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530312 } else {
313 vpnAdjRib.addVpn(nlriInfo, details, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700314 bgplocalRibVpn.add(sessionInfo(), nlriInfo, details,
Shashikanth VH3fe37982015-11-30 11:50:07 +0530315 ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530316 }
317 }
318 }
319 }
320
321 /**
322 * Sets BGP path attribute and NLRI details.
323 *
324 * @param nlriInfo MpReachNlri path attribute
325 * @param pathAttr list of BGP path attributes
326 * @return details object of PathAttrNlriDetails
327 * @throws BgpParseException throw exception
328 */
329 public PathAttrNlriDetails setPathAttrDetails(BgpLSNlri nlriInfo, List<BgpValueType> pathAttr)
330 throws BgpParseException {
331 PathAttrNlriDetails details = new PathAttrNlriDetails();
332 details.setProtocolID(nlriInfo.getProtocolId());
333 details.setIdentifier(nlriInfo.getIdentifier());
334 details.setPathAttribute(pathAttr);
335 return details;
336 }
337
338 /**
339 * Removes NLRI identifier node in a tree separately based on afi and safi.
340 *
341 * @param peerImpl BGP peer instance
342 * @param nlri NLRI information
Priyanka Bfc51c952016-03-26 14:30:33 +0530343 * @throws BgpParseException BGP parse exception
Priyanka Bc08e56d2015-11-27 15:28:33 +0530344 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530345 public void callRemove(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri) throws BgpParseException {
Priyanka Bc08e56d2015-11-27 15:28:33 +0530346 ListIterator<BgpLSNlri> listIterator = nlri.listIterator();
347 while (listIterator.hasNext()) {
348 BgpLSNlri nlriInfo = listIterator.next();
349 if (nlriInfo instanceof BgpNodeLSNlriVer4) {
350 if (!((BgpNodeLSNlriVer4) nlriInfo).isVpnPresent()) {
351 adjRib.remove(nlriInfo);
Jonathan Hart51539b82015-10-29 09:53:04 -0700352 bgplocalRib.delete(nlriInfo);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530353 } else {
354 vpnAdjRib.removeVpn(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700355 bgplocalRibVpn.delete(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530356 }
357 } else if (nlriInfo instanceof BgpLinkLsNlriVer4) {
358 if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) {
359 adjRib.remove(nlriInfo);
Jonathan Hart51539b82015-10-29 09:53:04 -0700360 bgplocalRib.delete(nlriInfo);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530361 } else {
362 vpnAdjRib.removeVpn(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700363 bgplocalRibVpn.delete(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530364 }
365 } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) {
366 if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) {
367 adjRib.remove(nlriInfo);
Jonathan Hart51539b82015-10-29 09:53:04 -0700368 bgplocalRib.delete(nlriInfo);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530369 } else {
370 vpnAdjRib.removeVpn(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700371 bgplocalRibVpn.delete(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530372 }
373 }
374 }
375 }
376
377 /**
378 * Return the adjacency RIB-IN.
379 *
380 * @return adjRib the adjacency RIB-IN
381 */
382 public AdjRibIn adjRib() {
383 return adjRib;
384 }
385
386 /**
387 * Return the adjacency RIB-IN with VPN.
388 *
389 * @return vpnAdjRib the adjacency RIB-IN with VPN
390 */
391 public VpnAdjRibIn vpnAdjRib() {
392 return vpnAdjRib;
Shashikanth VH6de20d32015-10-09 12:04:13 +0530393 }
394
Shashikanth VH3fe37982015-11-30 11:50:07 +0530395 /**
396 * Update localRIB on peer disconnect.
397 *
Priyanka Bfc51c952016-03-26 14:30:33 +0530398 * @throws BgpParseException while updating local RIB
Shashikanth VH3fe37982015-11-30 11:50:07 +0530399 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530400 public void updateLocalRibOnPeerDisconnect() throws BgpParseException {
Jonathan Hart51539b82015-10-29 09:53:04 -0700401 BgpLocalRibImpl localRib = (BgpLocalRibImpl) bgplocalRib;
402 BgpLocalRibImpl localRibVpn = (BgpLocalRibImpl) bgplocalRibVpn;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530403
Jonathan Hart51539b82015-10-29 09:53:04 -0700404 localRib.localRibUpdate(adjacencyRib());
405 localRibVpn.localRibUpdate(vpnAdjacencyRib());
Shashikanth VH3fe37982015-11-30 11:50:07 +0530406 }
407
Shashikanth VH6de20d32015-10-09 12:04:13 +0530408 // ************************
409 // Channel related
410 // ************************
411
412 @Override
413 public final void disconnectPeer() {
414 this.channel.close();
415 }
416
417 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530418 public final void sendMessage(BgpMessage m) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530419 log.debug("Sending message to {}", channel.getRemoteAddress());
420 try {
421 channel.write(Collections.singletonList(m));
422 this.pktStats.addOutPacket();
423 } catch (RejectedExecutionException e) {
424 log.warn(e.getMessage());
425 if (!e.getMessage().contains(SHUTDOWN_MSG)) {
426 throw e;
427 }
428 }
429 }
430
431 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530432 public final void sendMessage(List<BgpMessage> msgs) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530433 try {
434 channel.write(msgs);
435 this.pktStats.addOutPacket(msgs.size());
436 } catch (RejectedExecutionException e) {
437 log.warn(e.getMessage());
438 if (!e.getMessage().contains(SHUTDOWN_MSG)) {
439 throw e;
440 }
441 }
442 }
443
444 @Override
445 public final boolean isConnected() {
446 return this.connected;
447 }
448
449 @Override
450 public final void setConnected(boolean connected) {
451 this.connected = connected;
Jon Hall8c7b06a2017-02-22 13:37:33 -0800452 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530453
454 @Override
455 public final void setChannel(Channel channel) {
456 this.channel = channel;
457 final SocketAddress address = channel.getRemoteAddress();
458 if (address instanceof InetSocketAddress) {
459 final InetSocketAddress inetAddress = (InetSocketAddress) address;
460 final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress());
461 if (ipAddress.isIp4()) {
462 channelId = ipAddress.toString() + ':' + inetAddress.getPort();
463 } else {
464 channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort();
465 }
466 }
Jon Hall8c7b06a2017-02-22 13:37:33 -0800467 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530468
469 @Override
470 public final Channel getChannel() {
471 return this.channel;
Jon Hall8c7b06a2017-02-22 13:37:33 -0800472 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530473
474 @Override
475 public String channelId() {
476 return channelId;
477 }
478
Shashikanth VH6de20d32015-10-09 12:04:13 +0530479 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530480 public BgpFactory factory() {
481 return BgpFactories.getFactory(sessionInfo.remoteBgpVersion());
Shashikanth VH6de20d32015-10-09 12:04:13 +0530482 }
483
484 @Override
485 public boolean isHandshakeComplete() {
486 return isHandShakeComplete;
487 }
488
489 @Override
490 public String toString() {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530491 return MoreObjects.toStringHelper(getClass()).omitNullValues()
492 .add("channel", channelId())
Priyanka Bc08e56d2015-11-27 15:28:33 +0530493 .add("BgpId", sessionInfo().remoteBgpId()).toString();
Shashikanth VH6de20d32015-10-09 12:04:13 +0530494 }
495}