Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 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 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 22 | import org.jboss.netty.channel.Channel; |
| 23 | import org.onlab.packet.IpAddress; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 24 | import org.onosproject.bgp.controller.BgpController; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 25 | import org.onosproject.bgp.controller.BgpLocalRib; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 26 | import org.onosproject.bgp.controller.BgpPeer; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 27 | import org.onosproject.bgp.controller.BgpSessionInfo; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 28 | import org.onosproject.bgpio.exceptions.BgpParseException; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 29 | import org.onosproject.bgpio.protocol.BgpFactories; |
| 30 | import org.onosproject.bgpio.protocol.BgpFactory; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 31 | import org.onosproject.bgpio.protocol.BgpLSNlri; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 32 | import org.onosproject.bgpio.protocol.BgpMessage; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 33 | import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecNlri; |
| 34 | import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecRouteKey; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 35 | import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 36 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4; |
| 37 | import org.onosproject.bgpio.protocol.linkstate.BgpPrefixIPv4LSNlriVer4; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 38 | import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 39 | import org.onosproject.bgpio.types.AsPath; |
| 40 | import org.onosproject.bgpio.types.As4Path; |
Shashikanth VH | 5826066 | 2016-02-13 01:12:02 +0530 | [diff] [blame] | 41 | import org.onosproject.bgpio.types.BgpExtendedCommunity; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 42 | import org.onosproject.bgpio.types.BgpValueType; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 43 | import org.onosproject.bgpio.types.LocalPref; |
| 44 | import org.onosproject.bgpio.types.Med; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 45 | import org.onosproject.bgpio.types.MpReachNlri; |
| 46 | import org.onosproject.bgpio.types.MpUnReachNlri; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 47 | import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv; |
| 48 | import org.onosproject.bgpio.types.Origin; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 49 | import org.onosproject.bgpio.types.attr.WideCommunity; |
| 50 | import org.onosproject.bgpio.types.RpdCapabilityTlv; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 51 | import org.onosproject.bgpio.util.Constants; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | import org.slf4j.LoggerFactory; |
| 54 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 55 | import java.net.InetSocketAddress; |
| 56 | import java.net.SocketAddress; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 57 | import java.util.ArrayList; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 58 | import java.util.Collections; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 59 | import java.util.LinkedList; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 60 | import java.util.List; |
| 61 | import java.util.ListIterator; |
| 62 | import java.util.concurrent.RejectedExecutionException; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * BGPPeerImpl implements BGPPeer, maintains peer information and store updates in RIB . |
| 66 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 67 | public class BgpPeerImpl implements BgpPeer { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 68 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 69 | protected final Logger log = LoggerFactory.getLogger(BgpPeerImpl.class); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 70 | |
| 71 | private static final String SHUTDOWN_MSG = "Worker has already been shutdown"; |
| 72 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 73 | private BgpController bgpController; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 74 | private Channel channel; |
| 75 | protected String channelId; |
| 76 | private boolean connected; |
| 77 | protected boolean isHandShakeComplete = false; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 78 | private BgpSessionInfo sessionInfo; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 79 | private BgpPacketStatsImpl pktStats; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 80 | private BgpLocalRib bgplocalRib; |
| 81 | private BgpLocalRib bgplocalRibVpn; |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 82 | private AdjRibIn adjRib; |
| 83 | private VpnAdjRibIn vpnAdjRib; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 84 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 85 | /** |
| 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 VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 102 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 103 | @Override |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 104 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 115 | public BgpPeerImpl(BgpController bgpController, BgpSessionInfo sessionInfo, BgpPacketStatsImpl pktStats) { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 116 | this.bgpController = bgpController; |
| 117 | this.sessionInfo = sessionInfo; |
| 118 | this.pktStats = pktStats; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 119 | this.bgplocalRib = bgpController.bgpLocalRib(); |
| 120 | this.bgplocalRibVpn = bgpController.bgpLocalRibVpn(); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 121 | this.adjRib = new AdjRibIn(); |
| 122 | this.vpnAdjRib = new VpnAdjRibIn(); |
| 123 | } |
| 124 | |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 125 | /** |
| 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 VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 147 | } else if (tlv.getType() == RpdCapabilityTlv.TYPE) { |
| 148 | RpdCapabilityTlv temp = (RpdCapabilityTlv) tlv; |
| 149 | if ((temp.getAfi() == afi) && (temp.getSafi() == sAfi)) { |
| 150 | return true; |
| 151 | } |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 152 | } |
| 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 VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 163 | * @param wideCommunity for route policy |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 164 | */ |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 165 | public final void sendFlowSpecUpdateMessageToPeer(FlowSpecOperation operType, BgpFlowSpecRouteKey routeKey, |
| 166 | BgpFlowSpecNlri flowSpec, WideCommunity wideCommunity) { |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 167 | |
| 168 | List<BgpValueType> attributesList = new LinkedList<>(); |
| 169 | byte sessionType = sessionInfo.isIbgpSession() ? (byte) 0 : (byte) 1; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 170 | byte sAfi = Constants.VPN_SAFI_FLOWSPEC_VALUE; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 171 | |
| 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 VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 179 | |
| 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 VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 189 | log.debug("Peer do not support BGP flow spec capability", channel.getRemoteAddress()); |
| 190 | return; |
| 191 | } |
| 192 | |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 193 | if ((wideCommunity != null) && (isVpnRpdCapabilitySet)) { |
| 194 | sAfi = Constants.VPN_SAFI_FLOWSPEC_RDP_VALUE; |
| 195 | } else if (isVpnFsCapabilitySet) { |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 196 | sAfi = Constants.VPN_SAFI_FLOWSPEC_VALUE; |
| 197 | } |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 198 | attributesList.add(new Origin((byte) 0)); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 199 | |
| 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 VH | 5826066 | 2016-02-13 01:12:02 +0530 | [diff] [blame] | 224 | attributesList.add(new BgpExtendedCommunity(flowSpec.fsActionTlv())); |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 225 | attributesList.add(wideCommunity); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 226 | |
| 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 VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 241 | public void updateFlowSpec(FlowSpecOperation operType, BgpFlowSpecRouteKey routeKey, |
| 242 | BgpFlowSpecNlri flowSpec, WideCommunity wideCommunity) { |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 243 | Preconditions.checkNotNull(operType, "flow specification operation type cannot be null"); |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 244 | Preconditions.checkNotNull(routeKey, "flow specification prefix cannot be null"); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 245 | Preconditions.checkNotNull(flowSpec, "flow specification details cannot be null"); |
| 246 | Preconditions.checkNotNull(flowSpec.fsActionTlv(), "flow specification action cannot be null"); |
| 247 | |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 248 | sendFlowSpecUpdateMessageToPeer(operType, routeKey, flowSpec, wideCommunity); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 249 | } |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 250 | |
| 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 Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 284 | bgplocalRib.add(sessionInfo(), nlriInfo, details); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 285 | } else { |
| 286 | vpnAdjRib.addVpn(nlriInfo, details, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 287 | bgplocalRibVpn.add(sessionInfo(), nlriInfo, details, |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 288 | ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 289 | } |
| 290 | } else if (nlriInfo instanceof BgpLinkLsNlriVer4) { |
| 291 | PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr); |
| 292 | if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) { |
| 293 | adjRib.add(nlriInfo, details); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 294 | bgplocalRib.add(sessionInfo(), nlriInfo, details); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 295 | } else { |
| 296 | vpnAdjRib.addVpn(nlriInfo, details, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 297 | bgplocalRibVpn.add(sessionInfo(), nlriInfo, details, |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 298 | ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 299 | } |
| 300 | } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) { |
| 301 | PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr); |
| 302 | if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) { |
| 303 | adjRib.add(nlriInfo, details); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 304 | bgplocalRib.add(sessionInfo(), nlriInfo, details); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 305 | } else { |
| 306 | vpnAdjRib.addVpn(nlriInfo, details, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 307 | bgplocalRibVpn.add(sessionInfo(), nlriInfo, details, |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 308 | ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 309 | } |
| 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 B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 336 | * @throws BgpParseException BGP parse exception |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 337 | */ |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 338 | public void callRemove(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri) throws BgpParseException { |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 339 | 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 Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 345 | bgplocalRib.delete(nlriInfo); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 346 | } else { |
| 347 | vpnAdjRib.removeVpn(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 348 | bgplocalRibVpn.delete(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 349 | } |
| 350 | } else if (nlriInfo instanceof BgpLinkLsNlriVer4) { |
| 351 | if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) { |
| 352 | adjRib.remove(nlriInfo); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 353 | bgplocalRib.delete(nlriInfo); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 354 | } else { |
| 355 | vpnAdjRib.removeVpn(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 356 | bgplocalRibVpn.delete(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 357 | } |
| 358 | } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) { |
| 359 | if (!((BgpPrefixIPv4LSNlriVer4) 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, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 364 | bgplocalRibVpn.delete(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 365 | } |
| 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 VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 386 | } |
| 387 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 388 | /** |
| 389 | * Update localRIB on peer disconnect. |
| 390 | * |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 391 | * @throws BgpParseException while updating local RIB |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 392 | */ |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 393 | public void updateLocalRibOnPeerDisconnect() throws BgpParseException { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 394 | BgpLocalRibImpl localRib = (BgpLocalRibImpl) bgplocalRib; |
| 395 | BgpLocalRibImpl localRibVpn = (BgpLocalRibImpl) bgplocalRibVpn; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 396 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 397 | localRib.localRibUpdate(adjacencyRib()); |
| 398 | localRibVpn.localRibUpdate(vpnAdjacencyRib()); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 399 | } |
| 400 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 401 | // ************************ |
| 402 | // Channel related |
| 403 | // ************************ |
| 404 | |
| 405 | @Override |
| 406 | public final void disconnectPeer() { |
| 407 | this.channel.close(); |
| 408 | } |
| 409 | |
| 410 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 411 | public final void sendMessage(BgpMessage m) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 412 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 425 | public final void sendMessage(List<BgpMessage> msgs) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 426 | 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 VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 472 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 473 | public BgpFactory factory() { |
| 474 | return BgpFactories.getFactory(sessionInfo.remoteBgpVersion()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | @Override |
| 478 | public boolean isHandshakeComplete() { |
| 479 | return isHandShakeComplete; |
| 480 | } |
| 481 | |
| 482 | @Override |
| 483 | public String toString() { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 484 | return MoreObjects.toStringHelper(getClass()).omitNullValues() |
| 485 | .add("channel", channelId()) |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 486 | .add("BgpId", sessionInfo().remoteBgpId()).toString(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 487 | } |
| 488 | } |