blob: ce7a33cfc46946f88a2a12e2787111e9194fe46b [file] [log] [blame]
Shashikanth VH6de20d32015-10-09 12:04:13 +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
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
22import java.util.ArrayList;
23import java.util.LinkedList;
24import java.util.Map;
25import java.util.Set;
26
Shashikanth VH6de20d32015-10-09 12:04:13 +053027import org.jboss.netty.channel.Channel;
28import org.onlab.packet.IpAddress;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053029import org.onosproject.bgp.controller.BgpController;
Jonathan Hart51539b82015-10-29 09:53:04 -070030import org.onosproject.bgp.controller.BgpLocalRib;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053031import org.onosproject.bgp.controller.BgpPeer;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053032import org.onosproject.bgp.controller.BgpSessionInfo;
Priyanka Bc08e56d2015-11-27 15:28:33 +053033import org.onosproject.bgpio.exceptions.BgpParseException;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053034import org.onosproject.bgpio.protocol.BgpFactories;
35import org.onosproject.bgpio.protocol.BgpFactory;
Priyanka Bc08e56d2015-11-27 15:28:33 +053036import org.onosproject.bgpio.protocol.BgpLSNlri;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053037import org.onosproject.bgpio.protocol.BgpMessage;
Shashikanth VHafb2e002016-02-12 14:48:29 +053038import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecDetails;
39import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecPrefix;
Jonathan Hart51539b82015-10-29 09:53:04 -070040import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
Priyanka Bc08e56d2015-11-27 15:28:33 +053041import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
42import org.onosproject.bgpio.protocol.linkstate.BgpPrefixIPv4LSNlriVer4;
Priyanka Bc08e56d2015-11-27 15:28:33 +053043import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails;
Shashikanth VHafb2e002016-02-12 14:48:29 +053044import org.onosproject.bgpio.types.AsPath;
45import org.onosproject.bgpio.types.As4Path;
Shashikanth VH58260662016-02-13 01:12:02 +053046import org.onosproject.bgpio.types.BgpExtendedCommunity;
Priyanka Bc08e56d2015-11-27 15:28:33 +053047import org.onosproject.bgpio.types.BgpValueType;
Shashikanth VHafb2e002016-02-12 14:48:29 +053048import org.onosproject.bgpio.types.LocalPref;
49import org.onosproject.bgpio.types.Med;
Priyanka Bc08e56d2015-11-27 15:28:33 +053050import org.onosproject.bgpio.types.MpReachNlri;
51import org.onosproject.bgpio.types.MpUnReachNlri;
Shashikanth VHafb2e002016-02-12 14:48:29 +053052import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv;
53import org.onosproject.bgpio.types.Origin;
54import org.onosproject.bgpio.types.RouteDistinguisher;
55import org.onosproject.bgpio.util.Constants;
Shashikanth VH6de20d32015-10-09 12:04:13 +053056import org.slf4j.Logger;
57import org.slf4j.LoggerFactory;
58
Jonathan Hart51539b82015-10-29 09:53:04 -070059import java.net.InetSocketAddress;
60import java.net.SocketAddress;
61import java.util.Collections;
62import java.util.List;
63import java.util.ListIterator;
64import java.util.concurrent.RejectedExecutionException;
Shashikanth VH6de20d32015-10-09 12:04:13 +053065
66/**
67 * BGPPeerImpl implements BGPPeer, maintains peer information and store updates in RIB .
68 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053069public class BgpPeerImpl implements BgpPeer {
Shashikanth VH6de20d32015-10-09 12:04:13 +053070
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053071 protected final Logger log = LoggerFactory.getLogger(BgpPeerImpl.class);
Shashikanth VH6de20d32015-10-09 12:04:13 +053072
73 private static final String SHUTDOWN_MSG = "Worker has already been shutdown";
74
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053075 private BgpController bgpController;
Shashikanth VH6de20d32015-10-09 12:04:13 +053076 private Channel channel;
77 protected String channelId;
78 private boolean connected;
79 protected boolean isHandShakeComplete = false;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053080 private BgpSessionInfo sessionInfo;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053081 private BgpPacketStatsImpl pktStats;
Jonathan Hart51539b82015-10-29 09:53:04 -070082 private BgpLocalRib bgplocalRib;
83 private BgpLocalRib bgplocalRibVpn;
Priyanka Bc08e56d2015-11-27 15:28:33 +053084 private AdjRibIn adjRib;
85 private VpnAdjRibIn vpnAdjRib;
Shashikanth VHafb2e002016-02-12 14:48:29 +053086 private BgpFlowSpecRibOut flowSpecRibOut;
87 private BgpFlowSpecRibOut vpnFlowSpecRibOut;
88
89 /**
90 * Returns the flowSpec RIB out.
91 *
92 * @return flow Specification RIB out
93 */
94 public BgpFlowSpecRibOut flowSpecRibOut() {
95 return flowSpecRibOut;
96 }
97
98 /**
99 * Returns the VPN flowSpec RIB out.
100 *
101 * @return VPN flow Specification RIB out
102 */
103 public BgpFlowSpecRibOut vpnFlowSpecRibOut() {
104 return vpnFlowSpecRibOut;
105 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530106
Shashikanth VH3fe37982015-11-30 11:50:07 +0530107 /**
108 * Return the adjacency RIB-IN.
109 *
110 * @return adjRib the adjacency RIB-IN
111 */
112 public AdjRibIn adjacencyRib() {
113 return adjRib;
114 }
115
116 /**
117 * Return the adjacency RIB-IN with VPN.
118 *
119 * @return vpnAdjRib the adjacency RIB-IN with VPN
120 */
121 public VpnAdjRibIn vpnAdjacencyRib() {
122 return vpnAdjRib;
123 }
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530124
Shashikanth VH6de20d32015-10-09 12:04:13 +0530125 @Override
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530126 public BgpSessionInfo sessionInfo() {
127 return sessionInfo;
128 }
129
130 /**
131 * Initialize peer.
132 *
133 *@param bgpController controller instance
134 *@param sessionInfo bgp session info
135 *@param pktStats packet statistics
136 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530137 public BgpPeerImpl(BgpController bgpController, BgpSessionInfo sessionInfo, BgpPacketStatsImpl pktStats) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530138 this.bgpController = bgpController;
139 this.sessionInfo = sessionInfo;
140 this.pktStats = pktStats;
Jonathan Hart51539b82015-10-29 09:53:04 -0700141 this.bgplocalRib = bgpController.bgpLocalRib();
142 this.bgplocalRibVpn = bgpController.bgpLocalRibVpn();
Priyanka Bc08e56d2015-11-27 15:28:33 +0530143 this.adjRib = new AdjRibIn();
144 this.vpnAdjRib = new VpnAdjRibIn();
Shashikanth VHafb2e002016-02-12 14:48:29 +0530145 this.flowSpecRibOut = new BgpFlowSpecRibOut();
146 this.vpnFlowSpecRibOut = new BgpFlowSpecRibOut();
Priyanka Bc08e56d2015-11-27 15:28:33 +0530147 }
148
Shashikanth VHafb2e002016-02-12 14:48:29 +0530149 /**
150 * Check if peer support capability.
151 *
152 * @param type capability type
153 * @param afi address family identifier
154 * @param sAfi subsequent address family identifier
155 * @return true if capability is supported, otherwise false
156 */
157 public final boolean isCapabilitySupported(short type, short afi, byte sAfi) {
158
159 List<BgpValueType> capability = sessionInfo.remoteBgpCapability();
160 ListIterator<BgpValueType> listIterator = capability.listIterator();
161
162 while (listIterator.hasNext()) {
163 BgpValueType tlv = listIterator.next();
164
165 if (tlv.getType() == type) {
166 if (tlv.getType() == MultiProtocolExtnCapabilityTlv.TYPE) {
167 MultiProtocolExtnCapabilityTlv temp = (MultiProtocolExtnCapabilityTlv) tlv;
168 if ((temp.getAfi() == afi) && (temp.getSafi() == sAfi)) {
169 return true;
170 }
171 }
172 }
173 }
174 return false;
175 }
176
177 /**
178 * Send flow specification update message to peer.
179 *
180 * @param operType operation type
181 * @param flowSpec flow specification details
182 */
183 public final void sendFlowSpecUpdateMessageToPeer(FlowSpecOperation operType, BgpFlowSpecDetails flowSpec) {
184
185 List<BgpValueType> attributesList = new LinkedList<>();
186 byte sessionType = sessionInfo.isIbgpSession() ? (byte) 0 : (byte) 1;
187 byte sAfi = Constants.SAFI_FLOWSPEC_VALUE;
188
189 boolean isFsCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
190 Constants.AFI_FLOWSPEC_VALUE,
191 Constants.SAFI_FLOWSPEC_VALUE);
192
193 boolean isVpnFsCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
194 Constants.AFI_FLOWSPEC_VALUE,
195 Constants.VPN_SAFI_FLOWSPEC_VALUE);
196 if ((!isFsCapabilitySet) && (!isVpnFsCapabilitySet)) {
197 log.debug("Peer do not support BGP flow spec capability", channel.getRemoteAddress());
198 return;
199 }
200
201 if (isVpnFsCapabilitySet) {
202 sAfi = Constants.VPN_SAFI_FLOWSPEC_VALUE;
203 }
204
205 attributesList.add(new Origin(sessionType));
206
207 if (sessionType != 0) {
208 // EBGP
209 if (!bgpController.getConfig().getLargeASCapability()) {
210 List<Short> aspathSet = new ArrayList<>();
211 List<Short> aspathSeq = new ArrayList<>();
212 aspathSeq.add((short) bgpController.getConfig().getAsNumber());
213
214 AsPath asPath = new AsPath(aspathSet, aspathSeq);
215 attributesList.add(asPath);
216 } else {
217 List<Integer> aspathSet = new ArrayList<>();
218 List<Integer> aspathSeq = new ArrayList<>();
219 aspathSeq.add(bgpController.getConfig().getAsNumber());
220
221 As4Path as4Path = new As4Path(aspathSet, aspathSeq);
222 attributesList.add(as4Path);
223 }
224 attributesList.add(new Med(0));
225 } else {
226 attributesList.add(new AsPath());
227 attributesList.add(new Med(0));
228 attributesList.add(new LocalPref(100));
229 }
230
Shashikanth VH58260662016-02-13 01:12:02 +0530231 attributesList.add(new BgpExtendedCommunity(flowSpec.fsActionTlv()));
Shashikanth VHafb2e002016-02-12 14:48:29 +0530232
233 if (operType == FlowSpecOperation.ADD) {
234 attributesList.add(new MpReachNlri(flowSpec, Constants.AFI_FLOWSPEC_VALUE, sAfi));
235 } else if (operType == FlowSpecOperation.DELETE) {
236 attributesList.add(new MpUnReachNlri(flowSpec, Constants.AFI_FLOWSPEC_VALUE, sAfi));
237 }
238
239 BgpMessage msg = Controller.getBgpMessageFactory4().updateMessageBuilder()
240 .setBgpPathAttributes(attributesList).build();
241
242 log.debug("Sending Flow spec Update message to {}", channel.getRemoteAddress());
243 channel.write(Collections.singletonList(msg));
244 }
245
246 @Override
247 public void updateFlowSpec(FlowSpecOperation operType, BgpFlowSpecPrefix prefix, BgpFlowSpecDetails flowSpec) {
248 Preconditions.checkNotNull(operType, "flow specification operation type cannot be null");
249 Preconditions.checkNotNull(prefix, "flow specification prefix cannot be null");
250 Preconditions.checkNotNull(flowSpec, "flow specification details cannot be null");
251 Preconditions.checkNotNull(flowSpec.fsActionTlv(), "flow specification action cannot be null");
252
253 if (operType == FlowSpecOperation.ADD) {
254 if (flowSpec.routeDistinguisher() == null) {
255 flowSpecRibOut.add(prefix, flowSpec);
256 } else {
257 vpnFlowSpecRibOut.add(flowSpec.routeDistinguisher(), prefix, flowSpec);
258 }
259 } else if (operType == FlowSpecOperation.DELETE) {
260 if (flowSpec.routeDistinguisher() == null) {
261 flowSpecRibOut.delete(prefix);
262 } else {
263 vpnFlowSpecRibOut.delete(flowSpec.routeDistinguisher(), prefix);
264 }
265 }
266 sendFlowSpecUpdateMessageToPeer(operType, flowSpec);
267 }
Priyanka Bc08e56d2015-11-27 15:28:33 +0530268
269 @Override
270 public void buildAdjRibIn(List<BgpValueType> pathAttr) throws BgpParseException {
271 ListIterator<BgpValueType> iterator = pathAttr.listIterator();
272 while (iterator.hasNext()) {
273 BgpValueType attr = iterator.next();
274 if (attr instanceof MpReachNlri) {
275 List<BgpLSNlri> nlri = ((MpReachNlri) attr).mpReachNlri();
276 callAdd(this, nlri, pathAttr);
277 }
278 if (attr instanceof MpUnReachNlri) {
279 List<BgpLSNlri> nlri = ((MpUnReachNlri) attr).mpUnReachNlri();
280 callRemove(this, nlri);
281 }
282 }
283 }
284
285 /**
286 * Updates NLRI identifier node in a tree separately based on afi and safi.
287 *
288 * @param peerImpl BGP peer instance
289 * @param nlri MpReachNlri path attribute
290 * @param pathAttr list of BGP path attributes
291 * @throws BgpParseException throws exception
292 */
293 public void callAdd(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri, List<BgpValueType> pathAttr)
294 throws BgpParseException {
295 ListIterator<BgpLSNlri> listIterator = nlri.listIterator();
296 while (listIterator.hasNext()) {
297 BgpLSNlri nlriInfo = listIterator.next();
298 if (nlriInfo instanceof BgpNodeLSNlriVer4) {
299 PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
300 if (!((BgpNodeLSNlriVer4) nlriInfo).isVpnPresent()) {
301 adjRib.add(nlriInfo, details);
Jonathan Hart51539b82015-10-29 09:53:04 -0700302 bgplocalRib.add(sessionInfo(), nlriInfo, details);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530303 } else {
304 vpnAdjRib.addVpn(nlriInfo, details, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700305 bgplocalRibVpn.add(sessionInfo(), nlriInfo, details,
Shashikanth VH3fe37982015-11-30 11:50:07 +0530306 ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530307 }
308 } else if (nlriInfo instanceof BgpLinkLsNlriVer4) {
309 PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
310 if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) {
311 adjRib.add(nlriInfo, details);
Jonathan Hart51539b82015-10-29 09:53:04 -0700312 bgplocalRib.add(sessionInfo(), nlriInfo, details);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530313 } else {
314 vpnAdjRib.addVpn(nlriInfo, details, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700315 bgplocalRibVpn.add(sessionInfo(), nlriInfo, details,
Shashikanth VH3fe37982015-11-30 11:50:07 +0530316 ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530317 }
318 } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) {
319 PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
320 if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) {
321 adjRib.add(nlriInfo, details);
Jonathan Hart51539b82015-10-29 09:53:04 -0700322 bgplocalRib.add(sessionInfo(), nlriInfo, details);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530323 } else {
324 vpnAdjRib.addVpn(nlriInfo, details, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700325 bgplocalRibVpn.add(sessionInfo(), nlriInfo, details,
Shashikanth VH3fe37982015-11-30 11:50:07 +0530326 ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530327 }
328 }
329 }
330 }
331
332 /**
333 * Sets BGP path attribute and NLRI details.
334 *
335 * @param nlriInfo MpReachNlri path attribute
336 * @param pathAttr list of BGP path attributes
337 * @return details object of PathAttrNlriDetails
338 * @throws BgpParseException throw exception
339 */
340 public PathAttrNlriDetails setPathAttrDetails(BgpLSNlri nlriInfo, List<BgpValueType> pathAttr)
341 throws BgpParseException {
342 PathAttrNlriDetails details = new PathAttrNlriDetails();
343 details.setProtocolID(nlriInfo.getProtocolId());
344 details.setIdentifier(nlriInfo.getIdentifier());
345 details.setPathAttribute(pathAttr);
346 return details;
347 }
348
349 /**
350 * Removes NLRI identifier node in a tree separately based on afi and safi.
351 *
352 * @param peerImpl BGP peer instance
353 * @param nlri NLRI information
354 */
355 public void callRemove(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri) {
356 ListIterator<BgpLSNlri> listIterator = nlri.listIterator();
357 while (listIterator.hasNext()) {
358 BgpLSNlri nlriInfo = listIterator.next();
359 if (nlriInfo instanceof BgpNodeLSNlriVer4) {
360 if (!((BgpNodeLSNlriVer4) nlriInfo).isVpnPresent()) {
361 adjRib.remove(nlriInfo);
Jonathan Hart51539b82015-10-29 09:53:04 -0700362 bgplocalRib.delete(nlriInfo);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530363 } else {
364 vpnAdjRib.removeVpn(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700365 bgplocalRibVpn.delete(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530366 }
367 } else if (nlriInfo instanceof BgpLinkLsNlriVer4) {
368 if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) {
369 adjRib.remove(nlriInfo);
Jonathan Hart51539b82015-10-29 09:53:04 -0700370 bgplocalRib.delete(nlriInfo);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530371 } else {
372 vpnAdjRib.removeVpn(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700373 bgplocalRibVpn.delete(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530374 }
375 } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) {
376 if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) {
377 adjRib.remove(nlriInfo);
Jonathan Hart51539b82015-10-29 09:53:04 -0700378 bgplocalRib.delete(nlriInfo);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530379 } else {
380 vpnAdjRib.removeVpn(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700381 bgplocalRibVpn.delete(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530382 }
383 }
384 }
385 }
386
387 /**
388 * Return the adjacency RIB-IN.
389 *
390 * @return adjRib the adjacency RIB-IN
391 */
392 public AdjRibIn adjRib() {
393 return adjRib;
394 }
395
396 /**
397 * Return the adjacency RIB-IN with VPN.
398 *
399 * @return vpnAdjRib the adjacency RIB-IN with VPN
400 */
401 public VpnAdjRibIn vpnAdjRib() {
402 return vpnAdjRib;
Shashikanth VH6de20d32015-10-09 12:04:13 +0530403 }
404
Shashikanth VH3fe37982015-11-30 11:50:07 +0530405 /**
406 * Update localRIB on peer disconnect.
407 *
408 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700409 public void updateLocalRibOnPeerDisconnect() {
410 BgpLocalRibImpl localRib = (BgpLocalRibImpl) bgplocalRib;
411 BgpLocalRibImpl localRibVpn = (BgpLocalRibImpl) bgplocalRibVpn;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530412
Jonathan Hart51539b82015-10-29 09:53:04 -0700413 localRib.localRibUpdate(adjacencyRib());
414 localRibVpn.localRibUpdate(vpnAdjacencyRib());
Shashikanth VH3fe37982015-11-30 11:50:07 +0530415 }
416
Shashikanth VHafb2e002016-02-12 14:48:29 +0530417 /**
418 * Update peer flow specification RIB on peer disconnect.
419 *
420 */
421 public void updateFlowSpecOnPeerDisconnect() {
422
423 boolean isCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
424 Constants.AFI_FLOWSPEC_VALUE,
425 Constants.SAFI_FLOWSPEC_VALUE);
426 if (isCapabilitySet) {
427 Set<BgpFlowSpecPrefix> flowSpecKeys = flowSpecRibOut.flowSpecTree().keySet();
428 for (BgpFlowSpecPrefix key : flowSpecKeys) {
429 BgpFlowSpecDetails flowSpecDetails = flowSpecRibOut.flowSpecTree().get(key);
430 sendFlowSpecUpdateMessageToPeer(FlowSpecOperation.DELETE, flowSpecDetails);
431 }
432 }
433
434 boolean isVpnCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
435 Constants.AFI_FLOWSPEC_VALUE,
436 Constants.VPN_SAFI_FLOWSPEC_VALUE);
437 if (isVpnCapabilitySet) {
438 Set<RouteDistinguisher> flowSpecKeys = vpnFlowSpecRibOut.vpnFlowSpecTree().keySet();
439 for (RouteDistinguisher key : flowSpecKeys) {
440 Map<BgpFlowSpecPrefix, BgpFlowSpecDetails> fsTree = vpnFlowSpecRibOut.vpnFlowSpecTree().get(key);
441
442 Set<BgpFlowSpecPrefix> fsKeys = fsTree.keySet();
443 for (BgpFlowSpecPrefix fsKey : fsKeys) {
444 BgpFlowSpecDetails flowSpecDetails = vpnFlowSpecRibOut.flowSpecTree().get(fsKey);
445 sendFlowSpecUpdateMessageToPeer(FlowSpecOperation.DELETE, flowSpecDetails);
446 }
447 }
448 }
449 }
450
Shashikanth VH6de20d32015-10-09 12:04:13 +0530451 // ************************
452 // Channel related
453 // ************************
454
455 @Override
456 public final void disconnectPeer() {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530457 this.updateFlowSpecOnPeerDisconnect();
Shashikanth VH6de20d32015-10-09 12:04:13 +0530458 this.channel.close();
459 }
460
461 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530462 public final void sendMessage(BgpMessage m) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530463 log.debug("Sending message to {}", channel.getRemoteAddress());
464 try {
465 channel.write(Collections.singletonList(m));
466 this.pktStats.addOutPacket();
467 } catch (RejectedExecutionException e) {
468 log.warn(e.getMessage());
469 if (!e.getMessage().contains(SHUTDOWN_MSG)) {
470 throw e;
471 }
472 }
473 }
474
475 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530476 public final void sendMessage(List<BgpMessage> msgs) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530477 try {
478 channel.write(msgs);
479 this.pktStats.addOutPacket(msgs.size());
480 } catch (RejectedExecutionException e) {
481 log.warn(e.getMessage());
482 if (!e.getMessage().contains(SHUTDOWN_MSG)) {
483 throw e;
484 }
485 }
486 }
487
488 @Override
489 public final boolean isConnected() {
490 return this.connected;
491 }
492
493 @Override
494 public final void setConnected(boolean connected) {
495 this.connected = connected;
496 };
497
498 @Override
499 public final void setChannel(Channel channel) {
500 this.channel = channel;
501 final SocketAddress address = channel.getRemoteAddress();
502 if (address instanceof InetSocketAddress) {
503 final InetSocketAddress inetAddress = (InetSocketAddress) address;
504 final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress());
505 if (ipAddress.isIp4()) {
506 channelId = ipAddress.toString() + ':' + inetAddress.getPort();
507 } else {
508 channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort();
509 }
510 }
511 };
512
513 @Override
514 public final Channel getChannel() {
515 return this.channel;
516 };
517
518 @Override
519 public String channelId() {
520 return channelId;
521 }
522
Shashikanth VH6de20d32015-10-09 12:04:13 +0530523 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530524 public BgpFactory factory() {
525 return BgpFactories.getFactory(sessionInfo.remoteBgpVersion());
Shashikanth VH6de20d32015-10-09 12:04:13 +0530526 }
527
528 @Override
529 public boolean isHandshakeComplete() {
530 return isHandShakeComplete;
531 }
532
533 @Override
534 public String toString() {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530535 return MoreObjects.toStringHelper(getClass()).omitNullValues()
536 .add("channel", channelId())
Priyanka Bc08e56d2015-11-27 15:28:33 +0530537 .add("BgpId", sessionInfo().remoteBgpId()).toString();
Shashikanth VH6de20d32015-10-09 12:04:13 +0530538 }
539}