Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Satish K | e107e66 | 2015-09-21 19:00:17 +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 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 19 | import org.onosproject.bgp.controller.BgpCfg; |
| 20 | import org.onosproject.bgp.controller.BgpController; |
| 21 | import org.onosproject.bgp.controller.BgpId; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 22 | import org.onosproject.bgp.controller.BgpLinkListener; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 23 | import org.onosproject.bgp.controller.BgpLocalRib; |
Shashikanth VH | f62b5c0 | 2015-11-20 22:23:08 +0530 | [diff] [blame] | 24 | import org.onosproject.bgp.controller.BgpNodeListener; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 25 | import org.onosproject.bgp.controller.BgpPeer; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 26 | import org.onosproject.bgp.controller.BgpPeerManager; |
Maciej Skala | 3251a30 | 2019-03-26 09:16:55 +0100 | [diff] [blame] | 27 | import org.onosproject.bgp.controller.BgpPrefixListener; |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 28 | import org.onosproject.bgp.controller.BgpRouteListener; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 29 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 30 | import org.onosproject.bgpio.protocol.BgpMessage; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 31 | import org.onosproject.bgpio.protocol.BgpUpdateMsg; |
| 32 | import org.onosproject.bgpio.types.BgpValueType; |
| 33 | import org.onosproject.bgpio.types.MpReachNlri; |
| 34 | import org.onosproject.bgpio.types.MpUnReachNlri; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 35 | import org.osgi.service.component.annotations.Activate; |
| 36 | import org.osgi.service.component.annotations.Component; |
| 37 | import org.osgi.service.component.annotations.Deactivate; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 41 | import java.util.Iterator; |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 42 | import java.util.LinkedList; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 43 | import java.util.List; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 44 | import java.util.Map; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 45 | import java.util.Set; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 46 | import java.util.TreeMap; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 47 | import java.util.concurrent.ConcurrentHashMap; |
| 48 | import java.util.concurrent.CopyOnWriteArraySet; |
Ankur Aggarwal | 2d7ff64 | 2020-07-29 08:46:04 +0000 | [diff] [blame^] | 49 | import java.util.concurrent.Executors; |
| 50 | import java.util.concurrent.ScheduledFuture; |
| 51 | import java.util.concurrent.ScheduledExecutorService; |
| 52 | import java.util.concurrent.TimeUnit; |
| 53 | import java.util.concurrent.atomic.AtomicBoolean; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 54 | import java.util.concurrent.locks.Lock; |
| 55 | import java.util.concurrent.locks.ReentrantLock; |
| 56 | |
Ankur Aggarwal | 2d7ff64 | 2020-07-29 08:46:04 +0000 | [diff] [blame^] | 57 | import static org.onlab.util.Tools.groupedThreads; |
| 58 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 59 | @Component(immediate = true, service = BgpController.class) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 60 | public class BgpControllerImpl implements BgpController { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 61 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 62 | private static final Logger log = LoggerFactory.getLogger(BgpControllerImpl.class); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 63 | final Controller ctrl = new Controller(this); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 64 | protected ConcurrentHashMap<BgpId, BgpPeer> connectedPeers = new ConcurrentHashMap<BgpId, BgpPeer>(); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 65 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 66 | protected BgpPeerManagerImpl peerManager = new BgpPeerManagerImpl(); |
Shashikanth VH | f62b5c0 | 2015-11-20 22:23:08 +0530 | [diff] [blame] | 67 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 68 | private BgpLocalRib bgplocalRib = new BgpLocalRibImpl(this); |
| 69 | private BgpLocalRib bgplocalRibVpn = new BgpLocalRibImpl(this); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 70 | |
Shashikanth VH | f62b5c0 | 2015-11-20 22:23:08 +0530 | [diff] [blame] | 71 | protected Set<BgpNodeListener> bgpNodeListener = new CopyOnWriteArraySet<>(); |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 72 | protected Set<BgpLinkListener> bgpLinkListener = new CopyOnWriteArraySet<>(); |
Maciej Skala | 3251a30 | 2019-03-26 09:16:55 +0100 | [diff] [blame] | 73 | protected Set<BgpPrefixListener> bgpPrefixListener = new CopyOnWriteArraySet<>(); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 74 | protected BgpController bgpController; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 75 | private BgpConfig bgpconfig = new BgpConfig(this); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 76 | private List<String> activeExceptionList = new LinkedList(); |
| 77 | private LinkedList<String> closedExceptionList = new LinkedList<String>(); |
| 78 | private Map<String, List<String>> activeSessionExceptionMap = new TreeMap<>(); |
| 79 | private Map<String, List<String>> closedSessionExceptionMap = new TreeMap<>(); |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 80 | protected Set<BgpRouteListener> bgpRouteListener = new CopyOnWriteArraySet<>(); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 81 | |
Ankur Aggarwal | 2d7ff64 | 2020-07-29 08:46:04 +0000 | [diff] [blame^] | 82 | //IDs for timers |
| 83 | private static final int PERIODIC_TIMER = 1001; |
| 84 | private static final int WARMUP_TIMER = 1002; |
| 85 | private static final int COOLDOWN_TIMER = 1003; |
| 86 | |
| 87 | private static final int POOL_SIZE = 3; //Current pool size is 3 |
| 88 | private ScheduledExecutorService executor; |
| 89 | private ScheduledFuture<?> cooldownFuture; |
| 90 | private ScheduledFuture<?> periodicFuture; |
| 91 | private ScheduledFuture<?> warmupFuture; |
| 92 | |
| 93 | private AtomicBoolean hasTopologyChanged = new AtomicBoolean(false); |
| 94 | |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 95 | @Override |
| 96 | public void activeSessionExceptionAdd(String peerId, String exception) { |
| 97 | if (peerId != null) { |
| 98 | activeExceptionList.add(exception); |
| 99 | activeSessionExceptionMap.put(peerId, activeExceptionList); |
| 100 | } else { |
| 101 | log.debug("Peer Id is null"); |
| 102 | } |
| 103 | if (activeExceptionList.size() > 10) { |
| 104 | activeExceptionList.clear(); |
| 105 | activeExceptionList.add(exception); |
| 106 | activeSessionExceptionMap.put(peerId, activeExceptionList); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
| 111 | @Override |
| 112 | public void closedSessionExceptionAdd(String peerId, String exception) { |
| 113 | if (peerId != null) { |
| 114 | closedExceptionList.add(exception); |
| 115 | closedSessionExceptionMap.put(peerId, closedExceptionList); |
| 116 | } else { |
| 117 | log.debug("Peer Id is null"); |
| 118 | } |
| 119 | if (closedExceptionList.size() > 10) { |
| 120 | closedExceptionList.clear(); |
| 121 | closedExceptionList.add(exception); |
| 122 | closedSessionExceptionMap.put(peerId, closedExceptionList); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Map<String, List<String>> activeSessionMap() { |
| 128 | return activeSessionExceptionMap; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public Map<String, List<String>> closedSessionMap() { |
| 133 | return closedSessionExceptionMap; |
| 134 | } |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 135 | |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 136 | @Override |
| 137 | public void addRouteListener(BgpRouteListener listener) { |
| 138 | this.bgpRouteListener.add(listener); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void removeRouteListener(BgpRouteListener listener) { |
| 143 | this.bgpRouteListener.remove(listener); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Set<BgpRouteListener> routeListener() { |
| 148 | return bgpRouteListener; |
| 149 | } |
| 150 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 151 | @Activate |
| 152 | public void activate() { |
| 153 | this.ctrl.start(); |
Ankur Aggarwal | 2d7ff64 | 2020-07-29 08:46:04 +0000 | [diff] [blame^] | 154 | executor = Executors.newScheduledThreadPool( |
| 155 | POOL_SIZE, |
| 156 | groupedThreads("onos/apps/bgpcontroller", "bgp-rr-timer")); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 157 | log.info("Started"); |
| 158 | } |
| 159 | |
| 160 | @Deactivate |
| 161 | public void deactivate() { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 162 | activeSessionExceptionMap.clear(); |
| 163 | closedSessionExceptionMap.clear(); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 164 | // Close all connected peers |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 165 | closeConnectedPeers(); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 166 | this.ctrl.stop(); |
Ankur Aggarwal | 2d7ff64 | 2020-07-29 08:46:04 +0000 | [diff] [blame^] | 167 | executor.shutdown(); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 168 | log.info("Stopped"); |
| 169 | } |
| 170 | |
| 171 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 172 | public Iterable<BgpPeer> getPeers() { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 173 | return this.connectedPeers.values(); |
| 174 | } |
| 175 | |
| 176 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 177 | public BgpPeer getPeer(BgpId bgpId) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 178 | return this.connectedPeers.get(bgpId); |
| 179 | } |
| 180 | |
| 181 | @Override |
Shashikanth VH | f62b5c0 | 2015-11-20 22:23:08 +0530 | [diff] [blame] | 182 | public void addListener(BgpNodeListener listener) { |
| 183 | this.bgpNodeListener.add(listener); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void removeListener(BgpNodeListener listener) { |
| 188 | this.bgpNodeListener.remove(listener); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public Set<BgpNodeListener> listener() { |
| 193 | return bgpNodeListener; |
| 194 | } |
| 195 | |
| 196 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 197 | public void writeMsg(BgpId bgpId, BgpMessage msg) { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 198 | this.getPeer(bgpId).sendMessage(msg); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 202 | public void processBgpPacket(BgpId bgpId, BgpMessage msg) throws BgpParseException { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 203 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 204 | BgpPeer peer = getPeer(bgpId); |
| 205 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 206 | switch (msg.getType()) { |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 207 | case OPEN: |
| 208 | // TODO: Process Open message |
| 209 | break; |
| 210 | case KEEP_ALIVE: |
| 211 | // TODO: Process keepalive message |
| 212 | break; |
| 213 | case NOTIFICATION: |
| 214 | // TODO: Process notificatoin message |
| 215 | break; |
| 216 | case UPDATE: |
| 217 | BgpUpdateMsg updateMsg = (BgpUpdateMsg) msg; |
| 218 | List<BgpValueType> pathAttr = updateMsg.bgpPathAttributes().pathAttributes(); |
| 219 | if (pathAttr == null) { |
| 220 | log.debug("llPathAttr is null, cannot process update message"); |
| 221 | break; |
| 222 | } |
| 223 | Iterator<BgpValueType> listIterator = pathAttr.iterator(); |
| 224 | boolean isLinkstate = false; |
| 225 | boolean isEvpn = false; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 226 | |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 227 | while (listIterator.hasNext()) { |
| 228 | BgpValueType attr = listIterator.next(); |
| 229 | if (attr instanceof MpReachNlri) { |
| 230 | MpReachNlri mpReach = (MpReachNlri) attr; |
| 231 | if (mpReach.bgpFlowSpecNlri() == null |
| 232 | && mpReach.bgpEvpnNlri() == null) { |
| 233 | isLinkstate = true; |
| 234 | } |
| 235 | if (mpReach.bgpEvpnNlri() != null) { |
| 236 | isEvpn = true; |
| 237 | } |
| 238 | } else if (attr instanceof MpUnReachNlri) { |
| 239 | MpUnReachNlri mpUnReach = (MpUnReachNlri) attr; |
| 240 | if (mpUnReach.bgpFlowSpecNlri() == null |
| 241 | && mpUnReach.bgpEvpnNlri() == null) { |
| 242 | isLinkstate = true; |
| 243 | } |
| 244 | if (mpUnReach.bgpEvpnNlri() != null) { |
| 245 | isEvpn = true; |
| 246 | } |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 247 | } |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 248 | } |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame] | 249 | if (isLinkstate) { |
| 250 | peer.buildAdjRibIn(pathAttr); |
| 251 | } |
| 252 | if (isEvpn) { |
| 253 | for (BgpRouteListener listener : bgpRouteListener) { |
| 254 | listener.processRoute(bgpId, updateMsg); |
| 255 | } |
| 256 | } |
| 257 | break; |
| 258 | default: |
| 259 | // TODO: Process other message |
| 260 | break; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 264 | @Override |
| 265 | public void closeConnectedPeers() { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 266 | BgpPeer bgpPeer; |
| 267 | for (BgpId id : this.connectedPeers.keySet()) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 268 | bgpPeer = getPeer(id); |
| 269 | bgpPeer.disconnectPeer(); |
| 270 | } |
| 271 | } |
| 272 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 273 | /** |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 274 | * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which |
| 275 | * they are. |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 276 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 277 | public class BgpPeerManagerImpl implements BgpPeerManager { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 278 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 279 | private final Logger log = LoggerFactory.getLogger(BgpPeerManagerImpl.class); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 280 | private final Lock peerLock = new ReentrantLock(); |
| 281 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 282 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 283 | public boolean addConnectedPeer(BgpId bgpId, BgpPeer bgpPeer) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 284 | |
| 285 | if (connectedPeers.get(bgpId) != null) { |
| 286 | this.log.error("Trying to add connectedPeer but found previous " + "value for bgp ip: {}", |
| 287 | bgpId.toString()); |
| 288 | return false; |
| 289 | } else { |
| 290 | this.log.debug("Added Peer {}", bgpId.toString()); |
| 291 | connectedPeers.put(bgpId, bgpPeer); |
Ankur Aggarwal | 2d7ff64 | 2020-07-29 08:46:04 +0000 | [diff] [blame^] | 292 | |
| 293 | //If all timers are stopped, start periodic timer |
| 294 | this.log.info("Start periodic timer"); |
| 295 | if (bgpconfig.isRouteRefreshEnabled() |
| 296 | && (periodicFuture == null || periodicFuture.isCancelled()) |
| 297 | && (cooldownFuture == null || cooldownFuture.isCancelled()) |
| 298 | && (warmupFuture == null || warmupFuture.isCancelled())) { |
| 299 | periodicFuture = executor.schedule(periodicTimerTask, |
| 300 | bgpconfig.getRouteRefreshPeriodicTimer(), TimeUnit.SECONDS); |
| 301 | } |
| 302 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 303 | return true; |
| 304 | } |
| 305 | } |
| 306 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 307 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 308 | public boolean isPeerConnected(BgpId bgpId) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 309 | if (connectedPeers.get(bgpId) == null) { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 310 | this.log.error("Is peer connected: bgpIp {}.", bgpId.toString()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 311 | return false; |
| 312 | } |
| 313 | |
| 314 | return true; |
| 315 | } |
| 316 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 317 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 318 | public void removeConnectedPeer(BgpId bgpId) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 319 | connectedPeers.remove(bgpId); |
| 320 | } |
| 321 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 322 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 323 | public BgpPeer getPeer(BgpId bgpId) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 324 | return connectedPeers.get(bgpId); |
| 325 | } |
| 326 | |
| 327 | /** |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 328 | * Gets bgp peer instance. |
| 329 | * |
| 330 | * @param bgpController controller instance. |
| 331 | * @param sessionInfo bgp session info. |
| 332 | * @param pktStats packet statistics. |
| 333 | * @return BGPPeer peer instance. |
| 334 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 335 | public BgpPeer getBgpPeerInstance(BgpController bgpController, BgpSessionInfoImpl sessionInfo, |
| 336 | BgpPacketStatsImpl pktStats) { |
| 337 | BgpPeer bgpPeer = new BgpPeerImpl(bgpController, sessionInfo, pktStats); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 338 | return bgpPeer; |
| 339 | } |
| 340 | |
| 341 | } |
| 342 | |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 343 | /** |
| 344 | * Returns controller. |
| 345 | * |
| 346 | * @return controller |
| 347 | */ |
| 348 | public Controller controller() { |
| 349 | return this.ctrl; |
| 350 | } |
| 351 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 352 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 353 | public ConcurrentHashMap<BgpId, BgpPeer> connectedPeers() { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 354 | return connectedPeers; |
| 355 | } |
| 356 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 357 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 358 | public BgpPeerManagerImpl peerManager() { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 359 | return peerManager; |
| 360 | } |
| 361 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 362 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 363 | public BgpCfg getConfig() { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 364 | return this.bgpconfig; |
| 365 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 366 | |
| 367 | @Override |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 368 | public int connectedPeerCount() { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 369 | return connectedPeers.size(); |
| 370 | } |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 371 | |
| 372 | /** |
| 373 | * Gets the BGP local RIB. |
| 374 | * |
| 375 | * @return bgplocalRIB BGP local RIB. |
| 376 | */ |
| 377 | @Override |
| 378 | public BgpLocalRib bgpLocalRib() { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 379 | return bgplocalRib; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Gets the BGP local RIB with VPN. |
| 384 | * |
| 385 | * @return bgplocalRIBVpn BGP VPN local RIB . |
| 386 | */ |
| 387 | @Override |
| 388 | public BgpLocalRib bgpLocalRibVpn() { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 389 | return bgplocalRibVpn; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 390 | } |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 391 | |
| 392 | @Override |
| 393 | public void addLinkListener(BgpLinkListener listener) { |
| 394 | this.bgpLinkListener.add(listener); |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public void removeLinkListener(BgpLinkListener listener) { |
| 399 | this.bgpLinkListener.remove(listener); |
| 400 | } |
| 401 | |
| 402 | @Override |
| 403 | public Set<BgpLinkListener> linkListener() { |
| 404 | return bgpLinkListener; |
| 405 | } |
Maciej Skala | 3251a30 | 2019-03-26 09:16:55 +0100 | [diff] [blame] | 406 | |
| 407 | @Override |
| 408 | public void addPrefixListener(BgpPrefixListener listener) { |
| 409 | bgpPrefixListener.add(listener); |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public void removePrefixListener(BgpPrefixListener listener) { |
| 414 | bgpPrefixListener.remove(listener); |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public Set<BgpPrefixListener> prefixListener() { |
| 419 | return bgpPrefixListener; |
| 420 | } |
Ankur Aggarwal | 2d7ff64 | 2020-07-29 08:46:04 +0000 | [diff] [blame^] | 421 | |
| 422 | @Override |
| 423 | public void notifyTopologyChange() { |
| 424 | log.info("Topology change received"); |
| 425 | |
| 426 | hasTopologyChanged.set(true); |
| 427 | |
| 428 | //If cooldown timer is running, do nothing further because routeRefresh will be sent when it expires |
| 429 | if (cooldownFuture != null && !cooldownFuture.isCancelled()) { |
| 430 | log.debug("Do nothing : Cooldown timer running"); |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | //If warmup timer is running, refresh it. If not, start it |
| 435 | if (warmupFuture != null && !warmupFuture.isCancelled()) { |
| 436 | warmupFuture.cancel(true); |
| 437 | warmupFuture = null; |
| 438 | |
| 439 | warmupFuture = executor.schedule(warmupTimerTask, |
| 440 | bgpconfig.getRouteRefreshWarmupTimer(), TimeUnit.SECONDS); |
| 441 | |
| 442 | log.debug("Warmup timer running. Re-started warmup timer"); |
| 443 | return; |
| 444 | } else { |
| 445 | warmupFuture = executor.schedule(warmupTimerTask, |
| 446 | bgpconfig.getRouteRefreshWarmupTimer(), TimeUnit.SECONDS); |
| 447 | log.debug("Warmup timer started"); |
| 448 | return; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | protected void resetTimers() { |
| 453 | if (periodicFuture != null && !periodicFuture.isCancelled()) { |
| 454 | periodicFuture.cancel(true); |
| 455 | periodicFuture = null; |
| 456 | } |
| 457 | |
| 458 | if (warmupFuture != null && !warmupFuture.isCancelled()) { |
| 459 | warmupFuture.cancel(true); |
| 460 | warmupFuture = null; |
| 461 | } |
| 462 | |
| 463 | if (cooldownFuture != null && !cooldownFuture.isCancelled()) { |
| 464 | cooldownFuture.cancel(true); |
| 465 | cooldownFuture = null; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | protected synchronized void timerCallback(int timerId) { |
| 470 | switch (timerId) { |
| 471 | case PERIODIC_TIMER: |
| 472 | //Cancel periodic timer and run cooldown timer |
| 473 | periodicFuture.cancel(true); |
| 474 | periodicFuture = null; |
| 475 | |
| 476 | sendRouteRefreshToPeers(); |
| 477 | |
| 478 | //Cancel warmup timer if it is running |
| 479 | if (warmupFuture != null && !warmupFuture.isCancelled()) { |
| 480 | warmupFuture.cancel(true); |
| 481 | warmupFuture = null; |
| 482 | } |
| 483 | |
| 484 | cooldownFuture = executor.schedule(cooldownTimerTask, |
| 485 | bgpconfig.getRouteRefreshCooldownTimer(), TimeUnit.SECONDS); |
| 486 | log.debug("Cooldown timer started"); |
| 487 | break; |
| 488 | case WARMUP_TIMER: |
| 489 | //Send route refresh and start cooldown timer |
| 490 | warmupFuture.cancel(true); |
| 491 | warmupFuture = null; |
| 492 | |
| 493 | sendRouteRefreshToPeers(); |
| 494 | |
| 495 | cooldownFuture = executor.schedule(cooldownTimerTask, |
| 496 | bgpconfig.getRouteRefreshCooldownTimer(), TimeUnit.SECONDS); |
| 497 | //Cancel periodic timer, if it is running |
| 498 | if (periodicFuture != null && !periodicFuture.isCancelled()) { |
| 499 | periodicFuture.cancel(true); |
| 500 | periodicFuture = null; |
| 501 | } |
| 502 | log.debug("Cooldown timer started"); |
| 503 | break; |
| 504 | case COOLDOWN_TIMER: |
| 505 | //If hasTopologyChanged is true, we need to restart cooldown timer. |
| 506 | //Otherwise, start periodic timer |
| 507 | boolean hasTopologyChangedValue = hasTopologyChanged.get(); |
| 508 | |
| 509 | cooldownFuture.cancel(true); |
| 510 | cooldownFuture = null; |
| 511 | |
| 512 | if (hasTopologyChangedValue) { |
| 513 | sendRouteRefreshToPeers(); |
| 514 | cooldownFuture = executor.schedule(cooldownTimerTask, |
| 515 | bgpconfig.getRouteRefreshCooldownTimer(), TimeUnit.SECONDS); |
| 516 | log.debug("Cooldown timer started"); |
| 517 | } else { |
| 518 | periodicFuture = executor.schedule(periodicTimerTask, |
| 519 | bgpconfig.getRouteRefreshPeriodicTimer(), TimeUnit.SECONDS); |
| 520 | log.debug("Periodic timer started"); |
| 521 | } |
| 522 | break; |
| 523 | default: |
| 524 | log.error("Invalid timerId in callback"); |
| 525 | } |
| 526 | |
| 527 | } |
| 528 | |
| 529 | private synchronized void sendRouteRefreshToPeers() { |
| 530 | //Iterate over peers and send route refresh |
| 531 | connectedPeers.forEach((k, v) -> v.sendRouteRefreshMessage()); |
| 532 | |
| 533 | //Refresh hasTopologyChanged variable |
| 534 | hasTopologyChanged.set(false); |
| 535 | } |
| 536 | |
| 537 | private Runnable periodicTimerTask = new Runnable() { |
| 538 | @Override |
| 539 | public void run() { |
| 540 | log.debug("Periodic Timer Expired"); |
| 541 | timerCallback(PERIODIC_TIMER); |
| 542 | } |
| 543 | }; |
| 544 | |
| 545 | private Runnable cooldownTimerTask = new Runnable() { |
| 546 | @Override |
| 547 | public void run() { |
| 548 | log.info("Cooldown Timer Expired"); |
| 549 | timerCallback(COOLDOWN_TIMER); |
| 550 | } |
| 551 | }; |
| 552 | |
| 553 | private Runnable warmupTimerTask = new Runnable() { |
| 554 | @Override |
| 555 | public void run() { |
| 556 | log.debug("Warmup Timer Expired"); |
| 557 | timerCallback(WARMUP_TIMER); |
| 558 | } |
| 559 | }; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 560 | } |