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)) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 145 | log.debug("Multi prorotcol extension capabality TLV is true"); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 146 | return true; |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 147 | |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 148 | } |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 149 | } else if (tlv.getType() == RpdCapabilityTlv.TYPE) { |
| 150 | RpdCapabilityTlv temp = (RpdCapabilityTlv) tlv; |
| 151 | if ((temp.getAfi() == afi) && (temp.getSafi() == sAfi)) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 152 | log.debug("RPD capabality TLV is true"); |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 153 | return true; |
| 154 | } |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 158 | log.debug("IS capabality is not supported "); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Send flow specification update message to peer. |
| 164 | * |
| 165 | * @param operType operation type |
Shashikanth VH | 83f77e4 | 2016-05-26 20:34:56 +0530 | [diff] [blame] | 166 | * @param routeKey flow rule key |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 167 | * @param flowSpec flow specification details |
Shashikanth VH | 83f77e4 | 2016-05-26 20:34:56 +0530 | [diff] [blame] | 168 | * @param wideCommunity for route policy |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 169 | */ |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 170 | public final void sendFlowSpecUpdateMessageToPeer(FlowSpecOperation operType, BgpFlowSpecRouteKey routeKey, |
| 171 | BgpFlowSpecNlri flowSpec, WideCommunity wideCommunity) { |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 172 | |
| 173 | List<BgpValueType> attributesList = new LinkedList<>(); |
| 174 | byte sessionType = sessionInfo.isIbgpSession() ? (byte) 0 : (byte) 1; |
Shashikanth VH | e38c21e | 2016-05-26 21:37:25 +0530 | [diff] [blame] | 175 | byte sAfi = Constants.SAFI_FLOWSPEC_VALUE; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 176 | |
| 177 | boolean isFsCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE, |
| 178 | Constants.AFI_FLOWSPEC_VALUE, |
| 179 | Constants.SAFI_FLOWSPEC_VALUE); |
| 180 | |
| 181 | boolean isVpnFsCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE, |
| 182 | Constants.AFI_FLOWSPEC_VALUE, |
| 183 | Constants.VPN_SAFI_FLOWSPEC_VALUE); |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 184 | |
| 185 | boolean isRpdCapabilitySet = isCapabilitySupported(RpdCapabilityTlv.TYPE, |
| 186 | Constants.AFI_FLOWSPEC_RPD_VALUE, |
| 187 | Constants.SAFI_FLOWSPEC_RPD_VALUE); |
| 188 | |
| 189 | boolean isVpnRpdCapabilitySet = isCapabilitySupported(RpdCapabilityTlv.TYPE, |
| 190 | Constants.AFI_FLOWSPEC_RPD_VALUE, |
| 191 | Constants.VPN_SAFI_FLOWSPEC_RDP_VALUE); |
| 192 | |
| 193 | if ((!isFsCapabilitySet) && (!isVpnFsCapabilitySet) && (!isRpdCapabilitySet) && (!isVpnRpdCapabilitySet)) { |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 194 | log.debug("Peer do not support BGP flow spec capability", channel.getRemoteAddress()); |
| 195 | return; |
| 196 | } |
| 197 | |
Shashikanth VH | e38c21e | 2016-05-26 21:37:25 +0530 | [diff] [blame] | 198 | if (isVpnFsCapabilitySet) { |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 199 | sAfi = Constants.VPN_SAFI_FLOWSPEC_VALUE; |
Shashikanth VH | e38c21e | 2016-05-26 21:37:25 +0530 | [diff] [blame] | 200 | } else if (isVpnRpdCapabilitySet) { |
| 201 | sAfi = Constants.VPN_SAFI_FLOWSPEC_RDP_VALUE; |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 202 | } |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 203 | attributesList.add(new Origin((byte) 0)); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 204 | |
| 205 | if (sessionType != 0) { |
| 206 | // EBGP |
| 207 | if (!bgpController.getConfig().getLargeASCapability()) { |
| 208 | List<Short> aspathSet = new ArrayList<>(); |
| 209 | List<Short> aspathSeq = new ArrayList<>(); |
| 210 | aspathSeq.add((short) bgpController.getConfig().getAsNumber()); |
| 211 | |
| 212 | AsPath asPath = new AsPath(aspathSet, aspathSeq); |
| 213 | attributesList.add(asPath); |
| 214 | } else { |
| 215 | List<Integer> aspathSet = new ArrayList<>(); |
| 216 | List<Integer> aspathSeq = new ArrayList<>(); |
| 217 | aspathSeq.add(bgpController.getConfig().getAsNumber()); |
| 218 | |
| 219 | As4Path as4Path = new As4Path(aspathSet, aspathSeq); |
| 220 | attributesList.add(as4Path); |
| 221 | } |
| 222 | attributesList.add(new Med(0)); |
| 223 | } else { |
| 224 | attributesList.add(new AsPath()); |
| 225 | attributesList.add(new Med(0)); |
| 226 | attributesList.add(new LocalPref(100)); |
| 227 | } |
| 228 | |
Shashikanth VH | 5826066 | 2016-02-13 01:12:02 +0530 | [diff] [blame] | 229 | attributesList.add(new BgpExtendedCommunity(flowSpec.fsActionTlv())); |
Shashikanth VH | e38c21e | 2016-05-26 21:37:25 +0530 | [diff] [blame] | 230 | if (wideCommunity != null) { |
| 231 | attributesList.add(wideCommunity); |
| 232 | } |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 233 | |
| 234 | if (operType == FlowSpecOperation.ADD) { |
| 235 | attributesList.add(new MpReachNlri(flowSpec, Constants.AFI_FLOWSPEC_VALUE, sAfi)); |
| 236 | } else if (operType == FlowSpecOperation.DELETE) { |
| 237 | attributesList.add(new MpUnReachNlri(flowSpec, Constants.AFI_FLOWSPEC_VALUE, sAfi)); |
| 238 | } |
| 239 | |
| 240 | BgpMessage msg = Controller.getBgpMessageFactory4().updateMessageBuilder() |
| 241 | .setBgpPathAttributes(attributesList).build(); |
| 242 | |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 243 | log.debug("Sending flow spec update message to {}", channel.getRemoteAddress()); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 244 | channel.write(Collections.singletonList(msg)); |
| 245 | } |
| 246 | |
| 247 | @Override |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 248 | public void updateFlowSpec(FlowSpecOperation operType, BgpFlowSpecRouteKey routeKey, |
| 249 | BgpFlowSpecNlri flowSpec, WideCommunity wideCommunity) { |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 250 | Preconditions.checkNotNull(operType, "flow specification operation type cannot be null"); |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 251 | Preconditions.checkNotNull(routeKey, "flow specification prefix cannot be null"); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 252 | Preconditions.checkNotNull(flowSpec, "flow specification details cannot be null"); |
| 253 | Preconditions.checkNotNull(flowSpec.fsActionTlv(), "flow specification action cannot be null"); |
| 254 | |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 255 | sendFlowSpecUpdateMessageToPeer(operType, routeKey, flowSpec, wideCommunity); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 256 | } |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 257 | |
| 258 | @Override |
| 259 | public void buildAdjRibIn(List<BgpValueType> pathAttr) throws BgpParseException { |
| 260 | ListIterator<BgpValueType> iterator = pathAttr.listIterator(); |
| 261 | while (iterator.hasNext()) { |
| 262 | BgpValueType attr = iterator.next(); |
| 263 | if (attr instanceof MpReachNlri) { |
| 264 | List<BgpLSNlri> nlri = ((MpReachNlri) attr).mpReachNlri(); |
| 265 | callAdd(this, nlri, pathAttr); |
| 266 | } |
| 267 | if (attr instanceof MpUnReachNlri) { |
| 268 | List<BgpLSNlri> nlri = ((MpUnReachNlri) attr).mpUnReachNlri(); |
| 269 | callRemove(this, nlri); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Updates NLRI identifier node in a tree separately based on afi and safi. |
| 276 | * |
| 277 | * @param peerImpl BGP peer instance |
| 278 | * @param nlri MpReachNlri path attribute |
| 279 | * @param pathAttr list of BGP path attributes |
| 280 | * @throws BgpParseException throws exception |
| 281 | */ |
| 282 | public void callAdd(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri, List<BgpValueType> pathAttr) |
| 283 | throws BgpParseException { |
| 284 | ListIterator<BgpLSNlri> listIterator = nlri.listIterator(); |
| 285 | while (listIterator.hasNext()) { |
| 286 | BgpLSNlri nlriInfo = listIterator.next(); |
| 287 | if (nlriInfo instanceof BgpNodeLSNlriVer4) { |
| 288 | PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr); |
| 289 | if (!((BgpNodeLSNlriVer4) nlriInfo).isVpnPresent()) { |
| 290 | adjRib.add(nlriInfo, details); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 291 | bgplocalRib.add(sessionInfo(), nlriInfo, details); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 292 | } else { |
| 293 | vpnAdjRib.addVpn(nlriInfo, details, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 294 | bgplocalRibVpn.add(sessionInfo(), nlriInfo, details, |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 295 | ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 296 | } |
| 297 | } else if (nlriInfo instanceof BgpLinkLsNlriVer4) { |
| 298 | PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr); |
| 299 | if (!((BgpLinkLsNlriVer4) 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, ((BgpLinkLsNlriVer4) 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 | ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 306 | } |
| 307 | } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) { |
| 308 | PathAttrNlriDetails details = setPathAttrDetails(nlriInfo, pathAttr); |
| 309 | if (!((BgpPrefixIPv4LSNlriVer4) 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, ((BgpPrefixIPv4LSNlriVer4) 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 | ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Sets BGP path attribute and NLRI details. |
| 323 | * |
| 324 | * @param nlriInfo MpReachNlri path attribute |
| 325 | * @param pathAttr list of BGP path attributes |
| 326 | * @return details object of PathAttrNlriDetails |
| 327 | * @throws BgpParseException throw exception |
| 328 | */ |
| 329 | public PathAttrNlriDetails setPathAttrDetails(BgpLSNlri nlriInfo, List<BgpValueType> pathAttr) |
| 330 | throws BgpParseException { |
| 331 | PathAttrNlriDetails details = new PathAttrNlriDetails(); |
| 332 | details.setProtocolID(nlriInfo.getProtocolId()); |
| 333 | details.setIdentifier(nlriInfo.getIdentifier()); |
| 334 | details.setPathAttribute(pathAttr); |
| 335 | return details; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Removes NLRI identifier node in a tree separately based on afi and safi. |
| 340 | * |
| 341 | * @param peerImpl BGP peer instance |
| 342 | * @param nlri NLRI information |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 343 | * @throws BgpParseException BGP parse exception |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 344 | */ |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 345 | public void callRemove(BgpPeerImpl peerImpl, List<BgpLSNlri> nlri) throws BgpParseException { |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 346 | ListIterator<BgpLSNlri> listIterator = nlri.listIterator(); |
| 347 | while (listIterator.hasNext()) { |
| 348 | BgpLSNlri nlriInfo = listIterator.next(); |
| 349 | if (nlriInfo instanceof BgpNodeLSNlriVer4) { |
| 350 | if (!((BgpNodeLSNlriVer4) nlriInfo).isVpnPresent()) { |
| 351 | adjRib.remove(nlriInfo); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 352 | bgplocalRib.delete(nlriInfo); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 353 | } else { |
| 354 | vpnAdjRib.removeVpn(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 355 | bgplocalRibVpn.delete(nlriInfo, ((BgpNodeLSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 356 | } |
| 357 | } else if (nlriInfo instanceof BgpLinkLsNlriVer4) { |
| 358 | if (!((BgpLinkLsNlriVer4) nlriInfo).isVpnPresent()) { |
| 359 | adjRib.remove(nlriInfo); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 360 | bgplocalRib.delete(nlriInfo); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 361 | } else { |
| 362 | vpnAdjRib.removeVpn(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 363 | bgplocalRibVpn.delete(nlriInfo, ((BgpLinkLsNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 364 | } |
| 365 | } else if (nlriInfo instanceof BgpPrefixIPv4LSNlriVer4) { |
| 366 | if (!((BgpPrefixIPv4LSNlriVer4) nlriInfo).isVpnPresent()) { |
| 367 | adjRib.remove(nlriInfo); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 368 | bgplocalRib.delete(nlriInfo); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 369 | } else { |
| 370 | vpnAdjRib.removeVpn(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 371 | bgplocalRibVpn.delete(nlriInfo, ((BgpPrefixIPv4LSNlriVer4) nlriInfo).getRouteDistinguisher()); |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Return the adjacency RIB-IN. |
| 379 | * |
| 380 | * @return adjRib the adjacency RIB-IN |
| 381 | */ |
| 382 | public AdjRibIn adjRib() { |
| 383 | return adjRib; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Return the adjacency RIB-IN with VPN. |
| 388 | * |
| 389 | * @return vpnAdjRib the adjacency RIB-IN with VPN |
| 390 | */ |
| 391 | public VpnAdjRibIn vpnAdjRib() { |
| 392 | return vpnAdjRib; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 393 | } |
| 394 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 395 | /** |
| 396 | * Update localRIB on peer disconnect. |
| 397 | * |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 398 | * @throws BgpParseException while updating local RIB |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 399 | */ |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 400 | public void updateLocalRibOnPeerDisconnect() throws BgpParseException { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 401 | BgpLocalRibImpl localRib = (BgpLocalRibImpl) bgplocalRib; |
| 402 | BgpLocalRibImpl localRibVpn = (BgpLocalRibImpl) bgplocalRibVpn; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 403 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 404 | localRib.localRibUpdate(adjacencyRib()); |
| 405 | localRibVpn.localRibUpdate(vpnAdjacencyRib()); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 406 | } |
| 407 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 408 | // ************************ |
| 409 | // Channel related |
| 410 | // ************************ |
| 411 | |
| 412 | @Override |
| 413 | public final void disconnectPeer() { |
| 414 | this.channel.close(); |
| 415 | } |
| 416 | |
| 417 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 418 | public final void sendMessage(BgpMessage m) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 419 | log.debug("Sending message to {}", channel.getRemoteAddress()); |
| 420 | try { |
| 421 | channel.write(Collections.singletonList(m)); |
| 422 | this.pktStats.addOutPacket(); |
| 423 | } catch (RejectedExecutionException e) { |
| 424 | log.warn(e.getMessage()); |
| 425 | if (!e.getMessage().contains(SHUTDOWN_MSG)) { |
| 426 | throw e; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 432 | public final void sendMessage(List<BgpMessage> msgs) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 433 | try { |
| 434 | channel.write(msgs); |
| 435 | this.pktStats.addOutPacket(msgs.size()); |
| 436 | } catch (RejectedExecutionException e) { |
| 437 | log.warn(e.getMessage()); |
| 438 | if (!e.getMessage().contains(SHUTDOWN_MSG)) { |
| 439 | throw e; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public final boolean isConnected() { |
| 446 | return this.connected; |
| 447 | } |
| 448 | |
| 449 | @Override |
| 450 | public final void setConnected(boolean connected) { |
| 451 | this.connected = connected; |
Jon Hall | 8c7b06a | 2017-02-22 13:37:33 -0800 | [diff] [blame^] | 452 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 453 | |
| 454 | @Override |
| 455 | public final void setChannel(Channel channel) { |
| 456 | this.channel = channel; |
| 457 | final SocketAddress address = channel.getRemoteAddress(); |
| 458 | if (address instanceof InetSocketAddress) { |
| 459 | final InetSocketAddress inetAddress = (InetSocketAddress) address; |
| 460 | final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress()); |
| 461 | if (ipAddress.isIp4()) { |
| 462 | channelId = ipAddress.toString() + ':' + inetAddress.getPort(); |
| 463 | } else { |
| 464 | channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort(); |
| 465 | } |
| 466 | } |
Jon Hall | 8c7b06a | 2017-02-22 13:37:33 -0800 | [diff] [blame^] | 467 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 468 | |
| 469 | @Override |
| 470 | public final Channel getChannel() { |
| 471 | return this.channel; |
Jon Hall | 8c7b06a | 2017-02-22 13:37:33 -0800 | [diff] [blame^] | 472 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 473 | |
| 474 | @Override |
| 475 | public String channelId() { |
| 476 | return channelId; |
| 477 | } |
| 478 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 479 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 480 | public BgpFactory factory() { |
| 481 | return BgpFactories.getFactory(sessionInfo.remoteBgpVersion()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | @Override |
| 485 | public boolean isHandshakeComplete() { |
| 486 | return isHandShakeComplete; |
| 487 | } |
| 488 | |
| 489 | @Override |
| 490 | public String toString() { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 491 | return MoreObjects.toStringHelper(getClass()).omitNullValues() |
| 492 | .add("channel", channelId()) |
Priyanka B | c08e56d | 2015-11-27 15:28:33 +0530 | [diff] [blame] | 493 | .add("BgpId", sessionInfo().remoteBgpId()).toString(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 494 | } |
| 495 | } |