blob: 3d144655ab4bfebce6446e5fd013aa3243e77a79 [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)) {
145 return true;
146 }
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530147 } else if (tlv.getType() == RpdCapabilityTlv.TYPE) {
148 RpdCapabilityTlv temp = (RpdCapabilityTlv) tlv;
149 if ((temp.getAfi() == afi) && (temp.getSafi() == sAfi)) {
150 return true;
151 }
Shashikanth VHafb2e002016-02-12 14:48:29 +0530152 }
153 }
154 }
155 return false;
156 }
157
158 /**
159 * Send flow specification update message to peer.
160 *
161 * @param operType operation type
162 * @param flowSpec flow specification details
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530163 * @param wideCommunity for route policy
Shashikanth VHafb2e002016-02-12 14:48:29 +0530164 */
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530165 public final void sendFlowSpecUpdateMessageToPeer(FlowSpecOperation operType, BgpFlowSpecRouteKey routeKey,
166 BgpFlowSpecNlri flowSpec, WideCommunity wideCommunity) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530167
168 List<BgpValueType> attributesList = new LinkedList<>();
169 byte sessionType = sessionInfo.isIbgpSession() ? (byte) 0 : (byte) 1;
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530170 byte sAfi = Constants.VPN_SAFI_FLOWSPEC_VALUE;
Shashikanth VHafb2e002016-02-12 14:48:29 +0530171
172 boolean isFsCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
173 Constants.AFI_FLOWSPEC_VALUE,
174 Constants.SAFI_FLOWSPEC_VALUE);
175
176 boolean isVpnFsCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
177 Constants.AFI_FLOWSPEC_VALUE,
178 Constants.VPN_SAFI_FLOWSPEC_VALUE);
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530179
180 boolean isRpdCapabilitySet = isCapabilitySupported(RpdCapabilityTlv.TYPE,
181 Constants.AFI_FLOWSPEC_RPD_VALUE,
182 Constants.SAFI_FLOWSPEC_RPD_VALUE);
183
184 boolean isVpnRpdCapabilitySet = isCapabilitySupported(RpdCapabilityTlv.TYPE,
185 Constants.AFI_FLOWSPEC_RPD_VALUE,
186 Constants.VPN_SAFI_FLOWSPEC_RDP_VALUE);
187
188 if ((!isFsCapabilitySet) && (!isVpnFsCapabilitySet) && (!isRpdCapabilitySet) && (!isVpnRpdCapabilitySet)) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530189 log.debug("Peer do not support BGP flow spec capability", channel.getRemoteAddress());
190 return;
191 }
192
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530193 if ((wideCommunity != null) && (isVpnRpdCapabilitySet)) {
194 sAfi = Constants.VPN_SAFI_FLOWSPEC_RDP_VALUE;
195 } else if (isVpnFsCapabilitySet) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530196 sAfi = Constants.VPN_SAFI_FLOWSPEC_VALUE;
197 }
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530198 attributesList.add(new Origin((byte) 0));
Shashikanth VHafb2e002016-02-12 14:48:29 +0530199
200 if (sessionType != 0) {
201 // EBGP
202 if (!bgpController.getConfig().getLargeASCapability()) {
203 List<Short> aspathSet = new ArrayList<>();
204 List<Short> aspathSeq = new ArrayList<>();
205 aspathSeq.add((short) bgpController.getConfig().getAsNumber());
206
207 AsPath asPath = new AsPath(aspathSet, aspathSeq);
208 attributesList.add(asPath);
209 } else {
210 List<Integer> aspathSet = new ArrayList<>();
211 List<Integer> aspathSeq = new ArrayList<>();
212 aspathSeq.add(bgpController.getConfig().getAsNumber());
213
214 As4Path as4Path = new As4Path(aspathSet, aspathSeq);
215 attributesList.add(as4Path);
216 }
217 attributesList.add(new Med(0));
218 } else {
219 attributesList.add(new AsPath());
220 attributesList.add(new Med(0));
221 attributesList.add(new LocalPref(100));
222 }
223
Shashikanth VH58260662016-02-13 01:12:02 +0530224 attributesList.add(new BgpExtendedCommunity(flowSpec.fsActionTlv()));
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530225 attributesList.add(wideCommunity);
Shashikanth VHafb2e002016-02-12 14:48:29 +0530226
227 if (operType == FlowSpecOperation.ADD) {
228 attributesList.add(new MpReachNlri(flowSpec, Constants.AFI_FLOWSPEC_VALUE, sAfi));
229 } else if (operType == FlowSpecOperation.DELETE) {
230 attributesList.add(new MpUnReachNlri(flowSpec, Constants.AFI_FLOWSPEC_VALUE, sAfi));
231 }
232
233 BgpMessage msg = Controller.getBgpMessageFactory4().updateMessageBuilder()
234 .setBgpPathAttributes(attributesList).build();
235
236 log.debug("Sending Flow spec Update message to {}", channel.getRemoteAddress());
237 channel.write(Collections.singletonList(msg));
238 }
239
240 @Override
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530241 public void updateFlowSpec(FlowSpecOperation operType, BgpFlowSpecRouteKey routeKey,
242 BgpFlowSpecNlri flowSpec, WideCommunity wideCommunity) {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530243 Preconditions.checkNotNull(operType, "flow specification operation type cannot be null");
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530244 Preconditions.checkNotNull(routeKey, "flow specification prefix cannot be null");
Shashikanth VHafb2e002016-02-12 14:48:29 +0530245 Preconditions.checkNotNull(flowSpec, "flow specification details cannot be null");
246 Preconditions.checkNotNull(flowSpec.fsActionTlv(), "flow specification action cannot be null");
247
Shashikanth VH26fd38a2016-04-26 18:11:37 +0530248 sendFlowSpecUpdateMessageToPeer(operType, routeKey, flowSpec, wideCommunity);
Shashikanth VHafb2e002016-02-12 14:48:29 +0530249 }
Priyanka Bc08e56d2015-11-27 15:28:33 +0530250
251 @Override
252 public void buildAdjRibIn(List<BgpValueType> pathAttr) throws BgpParseException {
253 ListIterator<BgpValueType> iterator = pathAttr.listIterator();
254 while (iterator.hasNext()) {
255 BgpValueType attr = iterator.next();
256 if (attr instanceof MpReachNlri) {
257 List<BgpLSNlri> nlri = ((MpReachNlri) attr).mpReachNlri();
258 callAdd(this, nlri, pathAttr);
259 }
260 if (attr instanceof MpUnReachNlri) {
261 List<BgpLSNlri> nlri = ((MpUnReachNlri) attr).mpUnReachNlri();
262 callRemove(this, nlri);
263 }
264 }
265 }
266
267 /**
268 * Updates NLRI identifier node in a tree separately based on afi and safi.
269 *
270 * @param peerImpl BGP peer instance
271 * @param nlri MpReachNlri path attribute
272 * @param pathAttr list of BGP path attributes
273 * @throws BgpParseException throws exception
274 */
275 public void callAdd(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri, List<BgpValueType> pathAttr)
276 throws BgpParseException {
277 ListIterator<BgpLSNlri> listIterator = nlri.listIterator();
278 while (listIterator.hasNext()) {
279 BgpLSNlri nlriInfo = listIterator.next();
280 if (nlriInfo instanceof BgpNodeLSNlriVer4) {
281 PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
282 if (!((BgpNodeLSNlriVer4) nlriInfo).isVpnPresent()) {
283 adjRib.add(nlriInfo, details);
Jonathan Hart51539b82015-10-29 09:53:04 -0700284 bgplocalRib.add(sessionInfo(), nlriInfo, details);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530285 } else {
286 vpnAdjRib.addVpn(nlriInfo, details, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700287 bgplocalRibVpn.add(sessionInfo(), nlriInfo, details,
Shashikanth VH3fe37982015-11-30 11:50:07 +0530288 ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530289 }
290 } else if (nlriInfo instanceof BgpLinkLsNlriVer4) {
291 PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
292 if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) {
293 adjRib.add(nlriInfo, details);
Jonathan Hart51539b82015-10-29 09:53:04 -0700294 bgplocalRib.add(sessionInfo(), nlriInfo, details);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530295 } else {
296 vpnAdjRib.addVpn(nlriInfo, details, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700297 bgplocalRibVpn.add(sessionInfo(), nlriInfo, details,
Shashikanth VH3fe37982015-11-30 11:50:07 +0530298 ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530299 }
300 } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) {
301 PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
302 if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) {
303 adjRib.add(nlriInfo, details);
Jonathan Hart51539b82015-10-29 09:53:04 -0700304 bgplocalRib.add(sessionInfo(), nlriInfo, details);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530305 } else {
306 vpnAdjRib.addVpn(nlriInfo, details, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700307 bgplocalRibVpn.add(sessionInfo(), nlriInfo, details,
Shashikanth VH3fe37982015-11-30 11:50:07 +0530308 ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530309 }
310 }
311 }
312 }
313
314 /**
315 * Sets BGP path attribute and NLRI details.
316 *
317 * @param nlriInfo MpReachNlri path attribute
318 * @param pathAttr list of BGP path attributes
319 * @return details object of PathAttrNlriDetails
320 * @throws BgpParseException throw exception
321 */
322 public PathAttrNlriDetails setPathAttrDetails(BgpLSNlri nlriInfo, List<BgpValueType> pathAttr)
323 throws BgpParseException {
324 PathAttrNlriDetails details = new PathAttrNlriDetails();
325 details.setProtocolID(nlriInfo.getProtocolId());
326 details.setIdentifier(nlriInfo.getIdentifier());
327 details.setPathAttribute(pathAttr);
328 return details;
329 }
330
331 /**
332 * Removes NLRI identifier node in a tree separately based on afi and safi.
333 *
334 * @param peerImpl BGP peer instance
335 * @param nlri NLRI information
Priyanka Bfc51c952016-03-26 14:30:33 +0530336 * @throws BgpParseException BGP parse exception
Priyanka Bc08e56d2015-11-27 15:28:33 +0530337 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530338 public void callRemove(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri) throws BgpParseException {
Priyanka Bc08e56d2015-11-27 15:28:33 +0530339 ListIterator<BgpLSNlri> listIterator = nlri.listIterator();
340 while (listIterator.hasNext()) {
341 BgpLSNlri nlriInfo = listIterator.next();
342 if (nlriInfo instanceof BgpNodeLSNlriVer4) {
343 if (!((BgpNodeLSNlriVer4) nlriInfo).isVpnPresent()) {
344 adjRib.remove(nlriInfo);
Jonathan Hart51539b82015-10-29 09:53:04 -0700345 bgplocalRib.delete(nlriInfo);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530346 } else {
347 vpnAdjRib.removeVpn(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700348 bgplocalRibVpn.delete(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530349 }
350 } else if (nlriInfo instanceof BgpLinkLsNlriVer4) {
351 if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) {
352 adjRib.remove(nlriInfo);
Jonathan Hart51539b82015-10-29 09:53:04 -0700353 bgplocalRib.delete(nlriInfo);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530354 } else {
355 vpnAdjRib.removeVpn(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700356 bgplocalRibVpn.delete(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530357 }
358 } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) {
359 if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) {
360 adjRib.remove(nlriInfo);
Jonathan Hart51539b82015-10-29 09:53:04 -0700361 bgplocalRib.delete(nlriInfo);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530362 } else {
363 vpnAdjRib.removeVpn(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700364 bgplocalRibVpn.delete(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530365 }
366 }
367 }
368 }
369
370 /**
371 * Return the adjacency RIB-IN.
372 *
373 * @return adjRib the adjacency RIB-IN
374 */
375 public AdjRibIn adjRib() {
376 return adjRib;
377 }
378
379 /**
380 * Return the adjacency RIB-IN with VPN.
381 *
382 * @return vpnAdjRib the adjacency RIB-IN with VPN
383 */
384 public VpnAdjRibIn vpnAdjRib() {
385 return vpnAdjRib;
Shashikanth VH6de20d32015-10-09 12:04:13 +0530386 }
387
Shashikanth VH3fe37982015-11-30 11:50:07 +0530388 /**
389 * Update localRIB on peer disconnect.
390 *
Priyanka Bfc51c952016-03-26 14:30:33 +0530391 * @throws BgpParseException while updating local RIB
Shashikanth VH3fe37982015-11-30 11:50:07 +0530392 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530393 public void updateLocalRibOnPeerDisconnect() throws BgpParseException {
Jonathan Hart51539b82015-10-29 09:53:04 -0700394 BgpLocalRibImpl localRib = (BgpLocalRibImpl) bgplocalRib;
395 BgpLocalRibImpl localRibVpn = (BgpLocalRibImpl) bgplocalRibVpn;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530396
Jonathan Hart51539b82015-10-29 09:53:04 -0700397 localRib.localRibUpdate(adjacencyRib());
398 localRibVpn.localRibUpdate(vpnAdjacencyRib());
Shashikanth VH3fe37982015-11-30 11:50:07 +0530399 }
400
Shashikanth VH6de20d32015-10-09 12:04:13 +0530401 // ************************
402 // Channel related
403 // ************************
404
405 @Override
406 public final void disconnectPeer() {
407 this.channel.close();
408 }
409
410 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530411 public final void sendMessage(BgpMessage m) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530412 log.debug("Sending message to {}", channel.getRemoteAddress());
413 try {
414 channel.write(Collections.singletonList(m));
415 this.pktStats.addOutPacket();
416 } catch (RejectedExecutionException e) {
417 log.warn(e.getMessage());
418 if (!e.getMessage().contains(SHUTDOWN_MSG)) {
419 throw e;
420 }
421 }
422 }
423
424 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530425 public final void sendMessage(List<BgpMessage> msgs) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530426 try {
427 channel.write(msgs);
428 this.pktStats.addOutPacket(msgs.size());
429 } catch (RejectedExecutionException e) {
430 log.warn(e.getMessage());
431 if (!e.getMessage().contains(SHUTDOWN_MSG)) {
432 throw e;
433 }
434 }
435 }
436
437 @Override
438 public final boolean isConnected() {
439 return this.connected;
440 }
441
442 @Override
443 public final void setConnected(boolean connected) {
444 this.connected = connected;
445 };
446
447 @Override
448 public final void setChannel(Channel channel) {
449 this.channel = channel;
450 final SocketAddress address = channel.getRemoteAddress();
451 if (address instanceof InetSocketAddress) {
452 final InetSocketAddress inetAddress = (InetSocketAddress) address;
453 final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress());
454 if (ipAddress.isIp4()) {
455 channelId = ipAddress.toString() + ':' + inetAddress.getPort();
456 } else {
457 channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort();
458 }
459 }
460 };
461
462 @Override
463 public final Channel getChannel() {
464 return this.channel;
465 };
466
467 @Override
468 public String channelId() {
469 return channelId;
470 }
471
Shashikanth VH6de20d32015-10-09 12:04:13 +0530472 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530473 public BgpFactory factory() {
474 return BgpFactories.getFactory(sessionInfo.remoteBgpVersion());
Shashikanth VH6de20d32015-10-09 12:04:13 +0530475 }
476
477 @Override
478 public boolean isHandshakeComplete() {
479 return isHandShakeComplete;
480 }
481
482 @Override
483 public String toString() {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530484 return MoreObjects.toStringHelper(getClass()).omitNullValues()
485 .add("channel", channelId())
Priyanka Bc08e56d2015-11-27 15:28:33 +0530486 .add("BgpId", sessionInfo().remoteBgpId()).toString();
Shashikanth VH6de20d32015-10-09 12:04:13 +0530487 }
488}