Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.bgp.controller.impl; |
| 18 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame^] | 20 | import com.google.common.base.Preconditions; |
| 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.LinkedList; |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 27 | import org.jboss.netty.channel.Channel; |
| 28 | import org.onlab.packet.IpAddress; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 29 | import org.onosproject.bgp.controller.BgpController; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 30 | import org.onosproject.bgp.controller.BgpLocalRib; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 31 | import org.onosproject.bgp.controller.BgpPeer; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 32 | import org.onosproject.bgp.controller.BgpSessionInfo; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 33 | import org.onosproject.bgpio.exceptions.BgpParseException; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 34 | import org.onosproject.bgpio.protocol.BgpFactories; |
| 35 | import org.onosproject.bgpio.protocol.BgpFactory; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 36 | import org.onosproject.bgpio.protocol.BgpLSNlri; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 37 | import org.onosproject.bgpio.protocol.BgpMessage; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame^] | 38 | import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecDetails; |
| 39 | import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecPrefix; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 40 | import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 41 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4; |
| 42 | import org.onosproject.bgpio.protocol.linkstate.BgpPrefixIPv4LSNlriVer4; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 43 | import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame^] | 44 | import org.onosproject.bgpio.types.AsPath; |
| 45 | import org.onosproject.bgpio.types.As4Path; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 46 | import org.onosproject.bgpio.types.BgpValueType; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame^] | 47 | import org.onosproject.bgpio.types.LocalPref; |
| 48 | import org.onosproject.bgpio.types.Med; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 49 | import org.onosproject.bgpio.types.MpReachNlri; |
| 50 | import org.onosproject.bgpio.types.MpUnReachNlri; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame^] | 51 | import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv; |
| 52 | import org.onosproject.bgpio.types.Origin; |
| 53 | import org.onosproject.bgpio.types.RouteDistinguisher; |
| 54 | import org.onosproject.bgpio.util.Constants; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 55 | import org.slf4j.Logger; |
| 56 | import org.slf4j.LoggerFactory; |
| 57 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 58 | import java.net.InetSocketAddress; |
| 59 | import java.net.SocketAddress; |
| 60 | import java.util.Collections; |
| 61 | import java.util.List; |
| 62 | import java.util.ListIterator; |
| 63 | import java.util.concurrent.RejectedExecutionException; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * BGPPeerImpl implements BGPPeer, maintains peer information and store updates in RIB . |
| 67 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 68 | public class BgpPeerImpl implements BgpPeer { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 69 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 70 | protected final Logger log = LoggerFactory.getLogger(BgpPeerImpl.class); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 71 | |
| 72 | private static final String SHUTDOWN_MSG = "Worker has already been shutdown"; |
| 73 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 74 | private BgpController bgpController; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 75 | private Channel channel; |
| 76 | protected String channelId; |
| 77 | private boolean connected; |
| 78 | protected boolean isHandShakeComplete = false; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 79 | private BgpSessionInfo sessionInfo; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 80 | private BgpPacketStatsImpl pktStats; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 81 | private BgpLocalRib bgplocalRib; |
| 82 | private BgpLocalRib bgplocalRibVpn; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 83 | private AdjRibIn adjRib; |
| 84 | private VpnAdjRibIn vpnAdjRib; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame^] | 85 | 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 VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 105 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 106 | /** |
| 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 VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 123 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 124 | @Override |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 125 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 136 | public BgpPeerImpl(BgpController bgpController, BgpSessionInfo sessionInfo, BgpPacketStatsImpl pktStats) { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 137 | this.bgpController = bgpController; |
| 138 | this.sessionInfo = sessionInfo; |
| 139 | this.pktStats = pktStats; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 140 | this.bgplocalRib = bgpController.bgpLocalRib(); |
| 141 | this.bgplocalRibVpn = bgpController.bgpLocalRibVpn(); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 142 | this.adjRib = new AdjRibIn(); |
| 143 | this.vpnAdjRib = new VpnAdjRibIn(); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame^] | 144 | this.flowSpecRibOut = new BgpFlowSpecRibOut(); |
| 145 | this.vpnFlowSpecRibOut = new BgpFlowSpecRibOut(); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 146 | } |
| 147 | |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame^] | 148 | /** |
| 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 B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 267 | |
| 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 Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 301 | bgplocalRib.add(sessionInfo(), nlriInfo, details); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 302 | } else { |
| 303 | vpnAdjRib.addVpn(nlriInfo, details, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 304 | bgplocalRibVpn.add(sessionInfo(), nlriInfo, details, |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 305 | ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 306 | } |
| 307 | } else if (nlriInfo instanceof BgpLinkLsNlriVer4) { |
| 308 | PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr); |
| 309 | if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) { |
| 310 | adjRib.add(nlriInfo, details); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 311 | bgplocalRib.add(sessionInfo(), nlriInfo, details); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 312 | } else { |
| 313 | vpnAdjRib.addVpn(nlriInfo, details, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 314 | bgplocalRibVpn.add(sessionInfo(), nlriInfo, details, |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 315 | ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 316 | } |
| 317 | } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) { |
| 318 | PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr); |
| 319 | if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) { |
| 320 | adjRib.add(nlriInfo, details); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 321 | bgplocalRib.add(sessionInfo(), nlriInfo, details); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 322 | } else { |
| 323 | vpnAdjRib.addVpn(nlriInfo, details, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 324 | bgplocalRibVpn.add(sessionInfo(), nlriInfo, details, |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 325 | ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 326 | } |
| 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 Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 361 | bgplocalRib.delete(nlriInfo); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 362 | } else { |
| 363 | vpnAdjRib.removeVpn(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 364 | bgplocalRibVpn.delete(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 365 | } |
| 366 | } else if (nlriInfo instanceof BgpLinkLsNlriVer4) { |
| 367 | if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) { |
| 368 | adjRib.remove(nlriInfo); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 369 | bgplocalRib.delete(nlriInfo); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 370 | } else { |
| 371 | vpnAdjRib.removeVpn(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 372 | bgplocalRibVpn.delete(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 373 | } |
| 374 | } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) { |
| 375 | if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) { |
| 376 | adjRib.remove(nlriInfo); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 377 | bgplocalRib.delete(nlriInfo); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 378 | } else { |
| 379 | vpnAdjRib.removeVpn(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 380 | bgplocalRibVpn.delete(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 381 | } |
| 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 VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 402 | } |
| 403 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 404 | /** |
| 405 | * Update localRIB on peer disconnect. |
| 406 | * |
| 407 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 408 | public void updateLocalRibOnPeerDisconnect() { |
| 409 | BgpLocalRibImpl localRib = (BgpLocalRibImpl) bgplocalRib; |
| 410 | BgpLocalRibImpl localRibVpn = (BgpLocalRibImpl) bgplocalRibVpn; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 411 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 412 | localRib.localRibUpdate(adjacencyRib()); |
| 413 | localRibVpn.localRibUpdate(vpnAdjacencyRib()); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 414 | } |
| 415 | |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame^] | 416 | /** |
| 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 VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 450 | // ************************ |
| 451 | // Channel related |
| 452 | // ************************ |
| 453 | |
| 454 | @Override |
| 455 | public final void disconnectPeer() { |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame^] | 456 | this.updateFlowSpecOnPeerDisconnect(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 457 | this.channel.close(); |
| 458 | } |
| 459 | |
| 460 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 461 | public final void sendMessage(BgpMessage m) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 462 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 475 | public final void sendMessage(List<BgpMessage> msgs) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 476 | 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 VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 522 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 523 | public BgpFactory factory() { |
| 524 | return BgpFactories.getFactory(sessionInfo.remoteBgpVersion()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | @Override |
| 528 | public boolean isHandshakeComplete() { |
| 529 | return isHandShakeComplete; |
| 530 | } |
| 531 | |
| 532 | @Override |
| 533 | public String toString() { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 534 | return MoreObjects.toStringHelper(getClass()).omitNullValues() |
| 535 | .add("channel", channelId()) |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 536 | .add("BgpId", sessionInfo().remoteBgpId()).toString(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 537 | } |
| 538 | } |