blob: efd84fbee5307486b9dab47842350c7bf4997441 [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;
Priyanka Bc08e56d2015-11-27 15:28:33 +053046import org.onosproject.bgpio.types.BgpValueType;
Shashikanth VHafb2e002016-02-12 14:48:29 +053047import org.onosproject.bgpio.types.LocalPref;
48import org.onosproject.bgpio.types.Med;
Priyanka Bc08e56d2015-11-27 15:28:33 +053049import org.onosproject.bgpio.types.MpReachNlri;
50import org.onosproject.bgpio.types.MpUnReachNlri;
Shashikanth VHafb2e002016-02-12 14:48:29 +053051import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv;
52import org.onosproject.bgpio.types.Origin;
53import org.onosproject.bgpio.types.RouteDistinguisher;
54import org.onosproject.bgpio.util.Constants;
Shashikanth VH6de20d32015-10-09 12:04:13 +053055import org.slf4j.Logger;
56import org.slf4j.LoggerFactory;
57
Jonathan Hart51539b82015-10-29 09:53:04 -070058import java.net.InetSocketAddress;
59import java.net.SocketAddress;
60import java.util.Collections;
61import 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 VHafb2e002016-02-12 14:48:29 +053085 private BgpFlowSpecRibOut flowSpecRibOut;
86 private BgpFlowSpecRibOut vpnFlowSpecRibOut;
87
88 /**
89 * Returns the flowSpec RIB out.
90 *
91 * @return flow Specification RIB out
92 */
93 public BgpFlowSpecRibOut flowSpecRibOut() {
94 return flowSpecRibOut;
95 }
96
97 /**
98 * Returns the VPN flowSpec RIB out.
99 *
100 * @return VPN flow Specification RIB out
101 */
102 public BgpFlowSpecRibOut vpnFlowSpecRibOut() {
103 return vpnFlowSpecRibOut;
104 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530105
Shashikanth VH3fe37982015-11-30 11:50:07 +0530106 /**
107 * Return the adjacency RIB-IN.
108 *
109 * @return adjRib the adjacency RIB-IN
110 */
111 public AdjRibIn adjacencyRib() {
112 return adjRib;
113 }
114
115 /**
116 * Return the adjacency RIB-IN with VPN.
117 *
118 * @return vpnAdjRib the adjacency RIB-IN with VPN
119 */
120 public VpnAdjRibIn vpnAdjacencyRib() {
121 return vpnAdjRib;
122 }
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530123
Shashikanth VH6de20d32015-10-09 12:04:13 +0530124 @Override
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530125 public BgpSessionInfo sessionInfo() {
126 return sessionInfo;
127 }
128
129 /**
130 * Initialize peer.
131 *
132 *@param bgpController controller instance
133 *@param sessionInfo bgp session info
134 *@param pktStats packet statistics
135 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530136 public BgpPeerImpl(BgpController bgpController, BgpSessionInfo sessionInfo, BgpPacketStatsImpl pktStats) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530137 this.bgpController = bgpController;
138 this.sessionInfo = sessionInfo;
139 this.pktStats = pktStats;
Jonathan Hart51539b82015-10-29 09:53:04 -0700140 this.bgplocalRib = bgpController.bgpLocalRib();
141 this.bgplocalRibVpn = bgpController.bgpLocalRibVpn();
Priyanka Bc08e56d2015-11-27 15:28:33 +0530142 this.adjRib = new AdjRibIn();
143 this.vpnAdjRib = new VpnAdjRibIn();
Shashikanth VHafb2e002016-02-12 14:48:29 +0530144 this.flowSpecRibOut = new BgpFlowSpecRibOut();
145 this.vpnFlowSpecRibOut = new BgpFlowSpecRibOut();
Priyanka Bc08e56d2015-11-27 15:28:33 +0530146 }
147
Shashikanth VHafb2e002016-02-12 14:48:29 +0530148 /**
149 * Check if peer support capability.
150 *
151 * @param type capability type
152 * @param afi address family identifier
153 * @param sAfi subsequent address family identifier
154 * @return true if capability is supported, otherwise false
155 */
156 public final boolean isCapabilitySupported(short type, short afi, byte sAfi) {
157
158 List<BgpValueType> capability = sessionInfo.remoteBgpCapability();
159 ListIterator<BgpValueType> listIterator = capability.listIterator();
160
161 while (listIterator.hasNext()) {
162 BgpValueType tlv = listIterator.next();
163
164 if (tlv.getType() == type) {
165 if (tlv.getType() == MultiProtocolExtnCapabilityTlv.TYPE) {
166 MultiProtocolExtnCapabilityTlv temp = (MultiProtocolExtnCapabilityTlv) tlv;
167 if ((temp.getAfi() == afi) && (temp.getSafi() == sAfi)) {
168 return true;
169 }
170 }
171 }
172 }
173 return false;
174 }
175
176 /**
177 * Send flow specification update message to peer.
178 *
179 * @param operType operation type
180 * @param flowSpec flow specification details
181 */
182 public final void sendFlowSpecUpdateMessageToPeer(FlowSpecOperation operType, BgpFlowSpecDetails flowSpec) {
183
184 List<BgpValueType> attributesList = new LinkedList<>();
185 byte sessionType = sessionInfo.isIbgpSession() ? (byte) 0 : (byte) 1;
186 byte sAfi = Constants.SAFI_FLOWSPEC_VALUE;
187
188 boolean isFsCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
189 Constants.AFI_FLOWSPEC_VALUE,
190 Constants.SAFI_FLOWSPEC_VALUE);
191
192 boolean isVpnFsCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
193 Constants.AFI_FLOWSPEC_VALUE,
194 Constants.VPN_SAFI_FLOWSPEC_VALUE);
195 if ((!isFsCapabilitySet) && (!isVpnFsCapabilitySet)) {
196 log.debug("Peer do not support BGP flow spec capability", channel.getRemoteAddress());
197 return;
198 }
199
200 if (isVpnFsCapabilitySet) {
201 sAfi = Constants.VPN_SAFI_FLOWSPEC_VALUE;
202 }
203
204 attributesList.add(new Origin(sessionType));
205
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
230 // TODO: Update flow spec action
231
232 if (operType == FlowSpecOperation.ADD) {
233 attributesList.add(new MpReachNlri(flowSpec, Constants.AFI_FLOWSPEC_VALUE, sAfi));
234 } else if (operType == FlowSpecOperation.DELETE) {
235 attributesList.add(new MpUnReachNlri(flowSpec, Constants.AFI_FLOWSPEC_VALUE, sAfi));
236 }
237
238 BgpMessage msg = Controller.getBgpMessageFactory4().updateMessageBuilder()
239 .setBgpPathAttributes(attributesList).build();
240
241 log.debug("Sending Flow spec Update message to {}", channel.getRemoteAddress());
242 channel.write(Collections.singletonList(msg));
243 }
244
245 @Override
246 public void updateFlowSpec(FlowSpecOperation operType, BgpFlowSpecPrefix prefix, BgpFlowSpecDetails flowSpec) {
247 Preconditions.checkNotNull(operType, "flow specification operation type cannot be null");
248 Preconditions.checkNotNull(prefix, "flow specification prefix cannot be null");
249 Preconditions.checkNotNull(flowSpec, "flow specification details cannot be null");
250 Preconditions.checkNotNull(flowSpec.fsActionTlv(), "flow specification action cannot be null");
251
252 if (operType == FlowSpecOperation.ADD) {
253 if (flowSpec.routeDistinguisher() == null) {
254 flowSpecRibOut.add(prefix, flowSpec);
255 } else {
256 vpnFlowSpecRibOut.add(flowSpec.routeDistinguisher(), prefix, flowSpec);
257 }
258 } else if (operType == FlowSpecOperation.DELETE) {
259 if (flowSpec.routeDistinguisher() == null) {
260 flowSpecRibOut.delete(prefix);
261 } else {
262 vpnFlowSpecRibOut.delete(flowSpec.routeDistinguisher(), prefix);
263 }
264 }
265 sendFlowSpecUpdateMessageToPeer(operType, flowSpec);
266 }
Priyanka Bc08e56d2015-11-27 15:28:33 +0530267
268 @Override
269 public void buildAdjRibIn(List<BgpValueType> pathAttr) throws BgpParseException {
270 ListIterator<BgpValueType> iterator = pathAttr.listIterator();
271 while (iterator.hasNext()) {
272 BgpValueType attr = iterator.next();
273 if (attr instanceof MpReachNlri) {
274 List<BgpLSNlri> nlri = ((MpReachNlri) attr).mpReachNlri();
275 callAdd(this, nlri, pathAttr);
276 }
277 if (attr instanceof MpUnReachNlri) {
278 List<BgpLSNlri> nlri = ((MpUnReachNlri) attr).mpUnReachNlri();
279 callRemove(this, nlri);
280 }
281 }
282 }
283
284 /**
285 * Updates NLRI identifier node in a tree separately based on afi and safi.
286 *
287 * @param peerImpl BGP peer instance
288 * @param nlri MpReachNlri path attribute
289 * @param pathAttr list of BGP path attributes
290 * @throws BgpParseException throws exception
291 */
292 public void callAdd(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri, List<BgpValueType> pathAttr)
293 throws BgpParseException {
294 ListIterator<BgpLSNlri> listIterator = nlri.listIterator();
295 while (listIterator.hasNext()) {
296 BgpLSNlri nlriInfo = listIterator.next();
297 if (nlriInfo instanceof BgpNodeLSNlriVer4) {
298 PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
299 if (!((BgpNodeLSNlriVer4) 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, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700304 bgplocalRibVpn.add(sessionInfo(), nlriInfo, details,
Shashikanth VH3fe37982015-11-30 11:50:07 +0530305 ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530306 }
307 } else if (nlriInfo instanceof BgpLinkLsNlriVer4) {
308 PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
309 if (!((BgpLinkLsNlriVer4) 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, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700314 bgplocalRibVpn.add(sessionInfo(), nlriInfo, details,
Shashikanth VH3fe37982015-11-30 11:50:07 +0530315 ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530316 }
317 } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) {
318 PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr);
319 if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) {
320 adjRib.add(nlriInfo, details);
Jonathan Hart51539b82015-10-29 09:53:04 -0700321 bgplocalRib.add(sessionInfo(), nlriInfo, details);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530322 } else {
323 vpnAdjRib.addVpn(nlriInfo, details, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700324 bgplocalRibVpn.add(sessionInfo(), nlriInfo, details,
Shashikanth VH3fe37982015-11-30 11:50:07 +0530325 ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530326 }
327 }
328 }
329 }
330
331 /**
332 * Sets BGP path attribute and NLRI details.
333 *
334 * @param nlriInfo MpReachNlri path attribute
335 * @param pathAttr list of BGP path attributes
336 * @return details object of PathAttrNlriDetails
337 * @throws BgpParseException throw exception
338 */
339 public PathAttrNlriDetails setPathAttrDetails(BgpLSNlri nlriInfo, List<BgpValueType> pathAttr)
340 throws BgpParseException {
341 PathAttrNlriDetails details = new PathAttrNlriDetails();
342 details.setProtocolID(nlriInfo.getProtocolId());
343 details.setIdentifier(nlriInfo.getIdentifier());
344 details.setPathAttribute(pathAttr);
345 return details;
346 }
347
348 /**
349 * Removes NLRI identifier node in a tree separately based on afi and safi.
350 *
351 * @param peerImpl BGP peer instance
352 * @param nlri NLRI information
353 */
354 public void callRemove(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri) {
355 ListIterator<BgpLSNlri> listIterator = nlri.listIterator();
356 while (listIterator.hasNext()) {
357 BgpLSNlri nlriInfo = listIterator.next();
358 if (nlriInfo instanceof BgpNodeLSNlriVer4) {
359 if (!((BgpNodeLSNlriVer4) 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, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700364 bgplocalRibVpn.delete(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530365 }
366 } else if (nlriInfo instanceof BgpLinkLsNlriVer4) {
367 if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) {
368 adjRib.remove(nlriInfo);
Jonathan Hart51539b82015-10-29 09:53:04 -0700369 bgplocalRib.delete(nlriInfo);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530370 } else {
371 vpnAdjRib.removeVpn(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700372 bgplocalRibVpn.delete(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530373 }
374 } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) {
375 if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) {
376 adjRib.remove(nlriInfo);
Jonathan Hart51539b82015-10-29 09:53:04 -0700377 bgplocalRib.delete(nlriInfo);
Priyanka Bc08e56d2015-11-27 15:28:33 +0530378 } else {
379 vpnAdjRib.removeVpn(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Jonathan Hart51539b82015-10-29 09:53:04 -0700380 bgplocalRibVpn.delete(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher());
Priyanka Bc08e56d2015-11-27 15:28:33 +0530381 }
382 }
383 }
384 }
385
386 /**
387 * Return the adjacency RIB-IN.
388 *
389 * @return adjRib the adjacency RIB-IN
390 */
391 public AdjRibIn adjRib() {
392 return adjRib;
393 }
394
395 /**
396 * Return the adjacency RIB-IN with VPN.
397 *
398 * @return vpnAdjRib the adjacency RIB-IN with VPN
399 */
400 public VpnAdjRibIn vpnAdjRib() {
401 return vpnAdjRib;
Shashikanth VH6de20d32015-10-09 12:04:13 +0530402 }
403
Shashikanth VH3fe37982015-11-30 11:50:07 +0530404 /**
405 * Update localRIB on peer disconnect.
406 *
407 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700408 public void updateLocalRibOnPeerDisconnect() {
409 BgpLocalRibImpl localRib = (BgpLocalRibImpl) bgplocalRib;
410 BgpLocalRibImpl localRibVpn = (BgpLocalRibImpl) bgplocalRibVpn;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530411
Jonathan Hart51539b82015-10-29 09:53:04 -0700412 localRib.localRibUpdate(adjacencyRib());
413 localRibVpn.localRibUpdate(vpnAdjacencyRib());
Shashikanth VH3fe37982015-11-30 11:50:07 +0530414 }
415
Shashikanth VHafb2e002016-02-12 14:48:29 +0530416 /**
417 * Update peer flow specification RIB on peer disconnect.
418 *
419 */
420 public void updateFlowSpecOnPeerDisconnect() {
421
422 boolean isCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
423 Constants.AFI_FLOWSPEC_VALUE,
424 Constants.SAFI_FLOWSPEC_VALUE);
425 if (isCapabilitySet) {
426 Set<BgpFlowSpecPrefix> flowSpecKeys = flowSpecRibOut.flowSpecTree().keySet();
427 for (BgpFlowSpecPrefix key : flowSpecKeys) {
428 BgpFlowSpecDetails flowSpecDetails = flowSpecRibOut.flowSpecTree().get(key);
429 sendFlowSpecUpdateMessageToPeer(FlowSpecOperation.DELETE, flowSpecDetails);
430 }
431 }
432
433 boolean isVpnCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
434 Constants.AFI_FLOWSPEC_VALUE,
435 Constants.VPN_SAFI_FLOWSPEC_VALUE);
436 if (isVpnCapabilitySet) {
437 Set<RouteDistinguisher> flowSpecKeys = vpnFlowSpecRibOut.vpnFlowSpecTree().keySet();
438 for (RouteDistinguisher key : flowSpecKeys) {
439 Map<BgpFlowSpecPrefix, BgpFlowSpecDetails> fsTree = vpnFlowSpecRibOut.vpnFlowSpecTree().get(key);
440
441 Set<BgpFlowSpecPrefix> fsKeys = fsTree.keySet();
442 for (BgpFlowSpecPrefix fsKey : fsKeys) {
443 BgpFlowSpecDetails flowSpecDetails = vpnFlowSpecRibOut.flowSpecTree().get(fsKey);
444 sendFlowSpecUpdateMessageToPeer(FlowSpecOperation.DELETE, flowSpecDetails);
445 }
446 }
447 }
448 }
449
Shashikanth VH6de20d32015-10-09 12:04:13 +0530450 // ************************
451 // Channel related
452 // ************************
453
454 @Override
455 public final void disconnectPeer() {
Shashikanth VHafb2e002016-02-12 14:48:29 +0530456 this.updateFlowSpecOnPeerDisconnect();
Shashikanth VH6de20d32015-10-09 12:04:13 +0530457 this.channel.close();
458 }
459
460 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530461 public final void sendMessage(BgpMessage m) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530462 log.debug("Sending message to {}", channel.getRemoteAddress());
463 try {
464 channel.write(Collections.singletonList(m));
465 this.pktStats.addOutPacket();
466 } catch (RejectedExecutionException e) {
467 log.warn(e.getMessage());
468 if (!e.getMessage().contains(SHUTDOWN_MSG)) {
469 throw e;
470 }
471 }
472 }
473
474 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530475 public final void sendMessage(List<BgpMessage> msgs) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530476 try {
477 channel.write(msgs);
478 this.pktStats.addOutPacket(msgs.size());
479 } catch (RejectedExecutionException e) {
480 log.warn(e.getMessage());
481 if (!e.getMessage().contains(SHUTDOWN_MSG)) {
482 throw e;
483 }
484 }
485 }
486
487 @Override
488 public final boolean isConnected() {
489 return this.connected;
490 }
491
492 @Override
493 public final void setConnected(boolean connected) {
494 this.connected = connected;
495 };
496
497 @Override
498 public final void setChannel(Channel channel) {
499 this.channel = channel;
500 final SocketAddress address = channel.getRemoteAddress();
501 if (address instanceof InetSocketAddress) {
502 final InetSocketAddress inetAddress = (InetSocketAddress) address;
503 final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress());
504 if (ipAddress.isIp4()) {
505 channelId = ipAddress.toString() + ':' + inetAddress.getPort();
506 } else {
507 channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort();
508 }
509 }
510 };
511
512 @Override
513 public final Channel getChannel() {
514 return this.channel;
515 };
516
517 @Override
518 public String channelId() {
519 return channelId;
520 }
521
Shashikanth VH6de20d32015-10-09 12:04:13 +0530522 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530523 public BgpFactory factory() {
524 return BgpFactories.getFactory(sessionInfo.remoteBgpVersion());
Shashikanth VH6de20d32015-10-09 12:04:13 +0530525 }
526
527 @Override
528 public boolean isHandshakeComplete() {
529 return isHandShakeComplete;
530 }
531
532 @Override
533 public String toString() {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530534 return MoreObjects.toStringHelper(getClass()).omitNullValues()
535 .add("channel", channelId())
Priyanka Bc08e56d2015-11-27 15:28:33 +0530536 .add("BgpId", sessionInfo().remoteBgpId()).toString();
Shashikanth VH6de20d32015-10-09 12:04:13 +0530537 }
538}