blob: 9a2df47ce9bba9e7c058c305b2a1a0ceede43ca6 [file] [log] [blame]
Satish Ke107e662015-09-21 19:00:17 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Satish Ke107e662015-09-21 19:00:17 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.bgp.controller.impl;
18
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053019import org.onosproject.bgp.controller.BgpCfg;
20import org.onosproject.bgp.controller.BgpController;
21import org.onosproject.bgp.controller.BgpId;
Priyanka Bfc51c952016-03-26 14:30:33 +053022import org.onosproject.bgp.controller.BgpLinkListener;
Shashikanth VH3fe37982015-11-30 11:50:07 +053023import org.onosproject.bgp.controller.BgpLocalRib;
Shashikanth VHf62b5c02015-11-20 22:23:08 +053024import org.onosproject.bgp.controller.BgpNodeListener;
Jonathan Hart51539b82015-10-29 09:53:04 -070025import org.onosproject.bgp.controller.BgpPeer;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053026import org.onosproject.bgp.controller.BgpPeerManager;
Maciej Skala3251a302019-03-26 09:16:55 +010027import org.onosproject.bgp.controller.BgpPrefixListener;
Mohammad Shahid30fedc52017-08-09 11:49:40 +053028import org.onosproject.bgp.controller.BgpRouteListener;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053029import org.onosproject.bgpio.exceptions.BgpParseException;
30import org.onosproject.bgpio.protocol.BgpMessage;
Shashikanth VH3fe37982015-11-30 11:50:07 +053031import org.onosproject.bgpio.protocol.BgpUpdateMsg;
32import org.onosproject.bgpio.types.BgpValueType;
33import org.onosproject.bgpio.types.MpReachNlri;
34import org.onosproject.bgpio.types.MpUnReachNlri;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070035import org.osgi.service.component.annotations.Activate;
36import org.osgi.service.component.annotations.Component;
37import org.osgi.service.component.annotations.Deactivate;
Satish Ke107e662015-09-21 19:00:17 +053038import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
Jonathan Hart51539b82015-10-29 09:53:04 -070041import java.util.Iterator;
mohamedrahil00f6f262016-11-24 20:20:41 +053042import java.util.LinkedList;
Jonathan Hart51539b82015-10-29 09:53:04 -070043import java.util.List;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044import java.util.Map;
Jonathan Hart51539b82015-10-29 09:53:04 -070045import java.util.Set;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070046import java.util.TreeMap;
Jonathan Hart51539b82015-10-29 09:53:04 -070047import java.util.concurrent.ConcurrentHashMap;
48import java.util.concurrent.CopyOnWriteArraySet;
Ankur Aggarwal2d7ff642020-07-29 08:46:04 +000049import java.util.concurrent.Executors;
50import java.util.concurrent.ScheduledFuture;
51import java.util.concurrent.ScheduledExecutorService;
52import java.util.concurrent.TimeUnit;
53import java.util.concurrent.atomic.AtomicBoolean;
Jonathan Hart51539b82015-10-29 09:53:04 -070054import java.util.concurrent.locks.Lock;
55import java.util.concurrent.locks.ReentrantLock;
56
Ankur Aggarwal2d7ff642020-07-29 08:46:04 +000057import static org.onlab.util.Tools.groupedThreads;
58
Ray Milkeyd84f89b2018-08-17 14:54:17 -070059@Component(immediate = true, service = BgpController.class)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053060public class BgpControllerImpl implements BgpController {
Satish Ke107e662015-09-21 19:00:17 +053061
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053062 private static final Logger log = LoggerFactory.getLogger(BgpControllerImpl.class);
mohamedrahil00f6f262016-11-24 20:20:41 +053063 final Controller ctrl = new Controller(this);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053064 protected ConcurrentHashMap<BgpId, BgpPeer> connectedPeers = new ConcurrentHashMap<BgpId, BgpPeer>();
Satish Ke107e662015-09-21 19:00:17 +053065
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053066 protected BgpPeerManagerImpl peerManager = new BgpPeerManagerImpl();
Shashikanth VHf62b5c02015-11-20 22:23:08 +053067
Jonathan Hart51539b82015-10-29 09:53:04 -070068 private BgpLocalRib bgplocalRib = new BgpLocalRibImpl(this);
69 private BgpLocalRib bgplocalRibVpn = new BgpLocalRibImpl(this);
Shashikanth VH3fe37982015-11-30 11:50:07 +053070
Shashikanth VHf62b5c02015-11-20 22:23:08 +053071 protected Set<BgpNodeListener> bgpNodeListener = new CopyOnWriteArraySet<>();
Priyanka Bfc51c952016-03-26 14:30:33 +053072 protected Set<BgpLinkListener> bgpLinkListener = new CopyOnWriteArraySet<>();
Maciej Skala3251a302019-03-26 09:16:55 +010073 protected Set<BgpPrefixListener> bgpPrefixListener = new CopyOnWriteArraySet<>();
mohamedrahil00f6f262016-11-24 20:20:41 +053074 protected BgpController bgpController;
Shashikanth VH3fe37982015-11-30 11:50:07 +053075 private BgpConfig bgpconfig = new BgpConfig(this);
mohamedrahil00f6f262016-11-24 20:20:41 +053076 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 Shahid30fedc52017-08-09 11:49:40 +053080 protected Set<BgpRouteListener> bgpRouteListener = new CopyOnWriteArraySet<>();
mohamedrahil00f6f262016-11-24 20:20:41 +053081
Ankur Aggarwal2d7ff642020-07-29 08:46:04 +000082 //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
mohamedrahil00f6f262016-11-24 20:20:41 +053095 @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 Ke107e662015-09-21 19:00:17 +0530135
Mohammad Shahid30fedc52017-08-09 11:49:40 +0530136 @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 Ke107e662015-09-21 19:00:17 +0530151 @Activate
152 public void activate() {
153 this.ctrl.start();
Ankur Aggarwal2d7ff642020-07-29 08:46:04 +0000154 executor = Executors.newScheduledThreadPool(
155 POOL_SIZE,
156 groupedThreads("onos/apps/bgpcontroller", "bgp-rr-timer"));
Satish Ke107e662015-09-21 19:00:17 +0530157 log.info("Started");
158 }
159
160 @Deactivate
161 public void deactivate() {
mohamedrahil00f6f262016-11-24 20:20:41 +0530162 activeSessionExceptionMap.clear();
163 closedSessionExceptionMap.clear();
Satish Ke107e662015-09-21 19:00:17 +0530164 // Close all connected peers
Shashikanth VH6de20d32015-10-09 12:04:13 +0530165 closeConnectedPeers();
Satish Ke107e662015-09-21 19:00:17 +0530166 this.ctrl.stop();
Ankur Aggarwal2d7ff642020-07-29 08:46:04 +0000167 executor.shutdown();
Satish Ke107e662015-09-21 19:00:17 +0530168 log.info("Stopped");
169 }
170
171 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530172 public Iterable<BgpPeer> getPeers() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530173 return this.connectedPeers.values();
174 }
175
176 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530177 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530178 return this.connectedPeers.get(bgpId);
179 }
180
181 @Override
Shashikanth VHf62b5c02015-11-20 22:23:08 +0530182 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 VH5dd8dbe2015-11-26 13:22:18 +0530197 public void writeMsg(BgpId bgpId, BgpMessage msg) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530198 this.getPeer(bgpId).sendMessage(msg);
Satish Ke107e662015-09-21 19:00:17 +0530199 }
200
201 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -0700202 public void processBgpPacket(BgpId bgpId, BgpMessage msg) throws BgpParseException {
Satish Ke107e662015-09-21 19:00:17 +0530203
Shashikanth VH3fe37982015-11-30 11:50:07 +0530204 BgpPeer peer = getPeer(bgpId);
205
Satish Ke107e662015-09-21 19:00:17 +0530206 switch (msg.getType()) {
Mohammad Shahid30fedc52017-08-09 11:49:40 +0530207 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 VH26fd38a2016-04-26 18:11:37 +0530226
Mohammad Shahid30fedc52017-08-09 11:49:40 +0530227 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 VHafb2e002016-02-12 14:48:29 +0530247 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530248 }
Mohammad Shahid30fedc52017-08-09 11:49:40 +0530249 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 Ke107e662015-09-21 19:00:17 +0530261 }
262 }
263
Shashikanth VH6de20d32015-10-09 12:04:13 +0530264 @Override
265 public void closeConnectedPeers() {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530266 BgpPeer bgpPeer;
267 for (BgpId id : this.connectedPeers.keySet()) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530268 bgpPeer = getPeer(id);
269 bgpPeer.disconnectPeer();
270 }
271 }
272
Satish Ke107e662015-09-21 19:00:17 +0530273 /**
Shashikanth VH6de20d32015-10-09 12:04:13 +0530274 * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which
275 * they are.
Satish Ke107e662015-09-21 19:00:17 +0530276 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530277 public class BgpPeerManagerImpl implements BgpPeerManager {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530278
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530279 private final Logger log = LoggerFactory.getLogger(BgpPeerManagerImpl.class);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530280 private final Lock peerLock = new ReentrantLock();
281
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530282 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530283 public boolean addConnectedPeer(BgpId bgpId, BgpPeer bgpPeer) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530284
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 Aggarwal2d7ff642020-07-29 08:46:04 +0000292
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 VH6de20d32015-10-09 12:04:13 +0530303 return true;
304 }
305 }
306
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530307 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530308 public boolean isPeerConnected(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530309 if (connectedPeers.get(bgpId) == null) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530310 this.log.error("Is peer connected: bgpIp {}.", bgpId.toString());
Shashikanth VH6de20d32015-10-09 12:04:13 +0530311 return false;
312 }
313
314 return true;
315 }
316
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530317 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530318 public void removeConnectedPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530319 connectedPeers.remove(bgpId);
320 }
321
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530322 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530323 public BgpPeer getPeer(BgpId bgpId) {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530324 return connectedPeers.get(bgpId);
325 }
326
327 /**
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530328 * 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 VH5dd8dbe2015-11-26 13:22:18 +0530335 public BgpPeer getBgpPeerInstance(BgpController bgpController, BgpSessionInfoImpl sessionInfo,
336 BgpPacketStatsImpl pktStats) {
337 BgpPeer bgpPeer = new BgpPeerImpl(bgpController, sessionInfo, pktStats);
Shashikanth VH6de20d32015-10-09 12:04:13 +0530338 return bgpPeer;
339 }
340
341 }
342
Vidyashree Rama7bd3d782015-11-23 08:46:33 +0530343 /**
344 * Returns controller.
345 *
346 * @return controller
347 */
348 public Controller controller() {
349 return this.ctrl;
350 }
351
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530352 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530353 public ConcurrentHashMap<BgpId, BgpPeer> connectedPeers() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530354 return connectedPeers;
355 }
356
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530357 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530358 public BgpPeerManagerImpl peerManager() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530359 return peerManager;
360 }
361
Satish Ke107e662015-09-21 19:00:17 +0530362 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530363 public BgpCfg getConfig() {
Satish Ke107e662015-09-21 19:00:17 +0530364 return this.bgpconfig;
365 }
Shashikanth VH6de20d32015-10-09 12:04:13 +0530366
367 @Override
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530368 public int connectedPeerCount() {
Shashikanth VH6de20d32015-10-09 12:04:13 +0530369 return connectedPeers.size();
370 }
Shashikanth VH3fe37982015-11-30 11:50:07 +0530371
372 /**
373 * Gets the BGP local RIB.
374 *
375 * @return bgplocalRIB BGP local RIB.
376 */
377 @Override
378 public BgpLocalRib bgpLocalRib() {
Jonathan Hart51539b82015-10-29 09:53:04 -0700379 return bgplocalRib;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530380 }
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 Hart51539b82015-10-29 09:53:04 -0700389 return bgplocalRibVpn;
Shashikanth VH3fe37982015-11-30 11:50:07 +0530390 }
Priyanka Bfc51c952016-03-26 14:30:33 +0530391
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 Skala3251a302019-03-26 09:16:55 +0100406
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 Aggarwal2d7ff642020-07-29 08:46:04 +0000421
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 VH9f8afb42015-11-04 18:00:30 +0530560}