blob: bb4d1538c666111c4cb9447a59d027252e0f7a93 [file] [log] [blame]
Thejaswi N K6a4cd002015-09-21 17:19:55 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thejaswi N K6a4cd002015-09-21 17:19:55 +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 */
16package org.onosproject.bgp.controller.impl;
17
Thejaswi N K6a4cd002015-09-21 17:19:55 +053018import org.onlab.packet.Ip4Address;
Shashikanth VH3fe37982015-11-30 11:50:07 +053019import org.onlab.packet.IpAddress;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053020import org.onosproject.bgp.controller.BgpCfg;
Shashikanth VH3fe37982015-11-30 11:50:07 +053021import org.onosproject.bgp.controller.BgpConnectPeer;
22import org.onosproject.bgp.controller.BgpController;
23import org.onosproject.bgp.controller.BgpId;
24import org.onosproject.bgp.controller.BgpPeer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053025import org.onosproject.bgp.controller.BgpPeerCfg;
Shashikanth VH3fe37982015-11-30 11:50:07 +053026import org.onosproject.bgp.controller.impl.BgpControllerImpl.BgpPeerManagerImpl;
Thejaswi N K6a4cd002015-09-21 17:19:55 +053027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
Mohammad Shahid30fedc52017-08-09 11:49:40 +053030import java.util.ArrayList;
31import java.util.Iterator;
32import java.util.List;
33import java.util.Map.Entry;
34import java.util.Set;
35import java.util.TreeMap;
36
Thejaswi N K6a4cd002015-09-21 17:19:55 +053037/**
38 * Provides BGP configuration of this BGP speaker.
39 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053040public class BgpConfig implements BgpCfg {
Thejaswi N K6a4cd002015-09-21 17:19:55 +053041
Ray Milkey9c9cde42018-01-12 14:22:06 -080042 private static final Logger log = LoggerFactory.getLogger(BgpConfig.class);
Thejaswi N K6a4cd002015-09-21 17:19:55 +053043
44 private static final short DEFAULT_HOLD_TIMER = 120;
45 private static final short DEFAULT_CONN_RETRY_TIME = 120;
46 private static final short DEFAULT_CONN_RETRY_COUNT = 5;
mohamedrahil00f6f262016-11-24 20:20:41 +053047 private List<BgpConnectPeerImpl> peerList = new ArrayList();
Thejaswi N K6a4cd002015-09-21 17:19:55 +053048 private State state = State.INIT;
49 private int localAs;
50 private int maxSession;
51 private boolean lsCapability;
52 private short holdTime;
53 private boolean largeAs = false;
54 private int maxConnRetryTime;
55 private int maxConnRetryCount;
Shashikanth VH580bdeb2016-02-19 17:26:03 +053056 private FlowSpec flowSpec = FlowSpec.NONE;
Thejaswi N K6a4cd002015-09-21 17:19:55 +053057 private Ip4Address routerId = null;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053058 private TreeMap<String, BgpPeerCfg> bgpPeerTree = new TreeMap<>();
Shashikanth VH3fe37982015-11-30 11:50:07 +053059 private BgpConnectPeer connectPeer;
60 private BgpPeerManagerImpl peerManager;
61 private BgpController bgpController;
Shashikanth VHb650bfa2016-04-18 12:54:03 +053062 private boolean rpdCapability;
Mohammad Shahid30fedc52017-08-09 11:49:40 +053063 private boolean evpnCapability;
Thejaswi N K6a4cd002015-09-21 17:19:55 +053064
Ankur Aggarwalf363d1a2019-11-11 14:56:18 +053065 //Set default connection type as IPv4
66 private ConnectionType connectionType = ConnectionType.IPV4;
67
Ankur Aggarwal9deda612020-03-26 05:28:09 +000068 private boolean isRouteRefreshEnabled = false;
69 private long periodicTimer;
70 private long warmupTimer;
71 private long cooldownTimer;
72
Shashikanth VH3fe37982015-11-30 11:50:07 +053073 /*
Thejaswi N K6a4cd002015-09-21 17:19:55 +053074 * Constructor to initialize the values.
75 */
Shashikanth VH3fe37982015-11-30 11:50:07 +053076 public BgpConfig(BgpController bgpController) {
77 this.bgpController = bgpController;
78 this.peerManager = (BgpPeerManagerImpl) bgpController.peerManager();
Thejaswi N K6a4cd002015-09-21 17:19:55 +053079 this.holdTime = DEFAULT_HOLD_TIMER;
80 this.maxConnRetryTime = DEFAULT_CONN_RETRY_TIME;
81 this.maxConnRetryCount = DEFAULT_CONN_RETRY_COUNT;
82 }
83
84 @Override
85 public State getState() {
86 return state;
87 }
88
89 @Override
90 public void setState(State state) {
91 this.state = state;
92 }
93
94 @Override
95 public int getAsNumber() {
96 return this.localAs;
97 }
98
99 @Override
100 public void setAsNumber(int localAs) {
101
102 State localState = getState();
103 this.localAs = localAs;
104
105 /* Set configuration state */
106 if (localState == State.IP_CONFIGURED) {
107 setState(State.IP_AS_CONFIGURED);
108 } else {
109 setState(State.AS_CONFIGURED);
110 }
111 }
112
113 @Override
114 public int getMaxSession() {
115 return this.maxSession;
116 }
117
118 @Override
119 public void setMaxSession(int maxSession) {
120 this.maxSession = maxSession;
121 }
122
123 @Override
124 public boolean getLsCapability() {
125 return this.lsCapability;
126 }
127
128 @Override
129 public void setLsCapability(boolean lsCapability) {
130 this.lsCapability = lsCapability;
131 }
132
133 @Override
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530134 public FlowSpec flowSpecCapability() {
135 return this.flowSpec;
136 }
137
138 @Override
139 public void setFlowSpecCapability(FlowSpec flowSpec) {
140 this.flowSpec = flowSpec;
141 }
142
143 @Override
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530144 public boolean flowSpecRpdCapability() {
145 return this.rpdCapability;
146 }
147
148 @Override
149 public void setFlowSpecRpdCapability(boolean rpdCapability) {
150 this.rpdCapability = rpdCapability;
151 }
152
153 @Override
Mohammad Shahid30fedc52017-08-09 11:49:40 +0530154 public boolean getEvpnCapability() {
155 return this.evpnCapability;
156 }
157
158 @Override
159 public void setEvpnCapability(boolean evpnCapability) {
160 this.evpnCapability = evpnCapability;
161 }
162
163 @Override
Ankur Aggarwalf363d1a2019-11-11 14:56:18 +0530164 public ConnectionType connectionType() {
165 return this.connectionType;
166 }
167
168 @Override
169 public void setConnectionType(ConnectionType connectionType) {
170 this.connectionType = connectionType;
171 }
172
173 @Override
Ankur Aggarwal9deda612020-03-26 05:28:09 +0000174 public boolean isRouteRefreshEnabled() {
175 return this.isRouteRefreshEnabled;
176 }
177
178 @Override
179 public void setRouteRefreshEnabled(boolean isEnabled) {
180 this.isRouteRefreshEnabled = isEnabled;
181 }
182
183 @Override
184 public long getRouteRefreshPeriodicTimer() {
185 return this.periodicTimer;
186 }
187
188 @Override
189 public void setRouteRefreshPeriodicTimer(long periodicTimer) {
190 this.periodicTimer = periodicTimer;
191 }
192
193 @Override
194 public long getRouteRefreshWarmupTimer() {
195 return this.warmupTimer;
196 }
197
198 @Override
199 public void setRouteRefreshWarmupTimer(long warmupTimer) {
200 this.warmupTimer = warmupTimer;
201 }
202
203 @Override
204 public long getRouteRefreshCooldownTimer() {
205 return this.cooldownTimer;
206 }
207
208 @Override
209 public void setRouteRefreshCooldownTimer(long cooldownTimer) {
210 this.cooldownTimer = cooldownTimer;
211 }
212
213 @Override
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530214 public String getRouterId() {
215 if (this.routerId != null) {
216 return this.routerId.toString();
217 } else {
218 return null;
219 }
220 }
221
222 @Override
223 public void setRouterId(String routerId) {
224 State localState = getState();
225 this.routerId = Ip4Address.valueOf(routerId);
226
227 /* Set configuration state */
228 if (localState == State.AS_CONFIGURED) {
229 setState(State.IP_AS_CONFIGURED);
230 } else {
231 setState(State.IP_CONFIGURED);
232 }
233 }
234
235 @Override
236 public boolean addPeer(String routerid, int remoteAs) {
237 return addPeer(routerid, remoteAs, DEFAULT_HOLD_TIMER);
238 }
239
240 @Override
241 public boolean addPeer(String routerid, short holdTime) {
242 return addPeer(routerid, this.getAsNumber(), holdTime);
243 }
244
245 @Override
246 public boolean addPeer(String routerid, int remoteAs, short holdTime) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530247 BgpPeerConfig lspeer = new BgpPeerConfig();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530248 if (this.bgpPeerTree.get(routerid) == null) {
249
250 lspeer.setPeerRouterId(routerid);
251 lspeer.setAsNumber(remoteAs);
252 lspeer.setHoldtime(holdTime);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530253 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530254 lspeer.setSelfInnitConnection(false);
255
256 if (this.getAsNumber() == remoteAs) {
257 lspeer.setIsIBgp(true);
258 } else {
259 lspeer.setIsIBgp(false);
260 }
261
262 this.bgpPeerTree.put(routerid, lspeer);
mohamedrahil00f6f262016-11-24 20:20:41 +0530263 log.debug("Added successfully");
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530264 return true;
265 } else {
mohamedrahil00f6f262016-11-24 20:20:41 +0530266 log.debug("Already exists");
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530267 return false;
268 }
269 }
270
271 @Override
272 public boolean connectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530273 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530274
275 if (lspeer != null) {
276 lspeer.setSelfInnitConnection(true);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530277
278 if (lspeer.connectPeer() == null) {
Shashikanth VH83f77e42016-05-26 20:34:56 +0530279 connectPeer = new BgpConnectPeerImpl(bgpController, routerid, Controller.BGP_PORT_NUM);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530280 lspeer.setConnectPeer(connectPeer);
281 connectPeer.connectPeer();
mohamedrahil00f6f262016-11-24 20:20:41 +0530282 peerList.add((BgpConnectPeerImpl) connectPeer);
283
Shashikanth VH3fe37982015-11-30 11:50:07 +0530284 }
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530285 return true;
286 }
287
288 return false;
289 }
290
291 @Override
292 public boolean removePeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530293 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530294
295 if (lspeer != null) {
296
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530297 disconnectPeer(routerid);
298 lspeer.setSelfInnitConnection(false);
299 lspeer = this.bgpPeerTree.remove(routerid);
300 log.debug("Deleted : " + routerid + " successfully");
301
302 return true;
303 } else {
304 log.debug("Did not find : " + routerid);
305 return false;
306 }
307 }
308
309 @Override
310 public boolean disconnectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530311 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530312
313 if (lspeer != null) {
314
Shashikanth VH3fe37982015-11-30 11:50:07 +0530315 BgpPeer disconnPeer = peerManager.getPeer(BgpId.bgpId(IpAddress.valueOf(routerid)));
316 if (disconnPeer != null) {
317 // TODO: send notification peer deconfigured
318 disconnPeer.disconnectPeer();
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530319 } else if (lspeer.connectPeer() != null) {
Shashikanth VH97e571e2016-01-05 12:15:14 +0530320 lspeer.connectPeer().disconnectPeer();
Shashikanth VH3fe37982015-11-30 11:50:07 +0530321 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530322 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530323 lspeer.setSelfInnitConnection(false);
324 log.debug("Disconnected : " + routerid + " successfully");
325
326 return true;
327 } else {
328 log.debug("Did not find : " + routerid);
329 return false;
330 }
331 }
332
333 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530334 public void setPeerConnState(String routerid, BgpPeerCfg.State state) {
335 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530336
337 if (lspeer != null) {
338 lspeer.setState(state);
339 log.debug("Peer : " + routerid + " is not available");
340
341 return;
342 } else {
343 log.debug("Did not find : " + routerid);
344 return;
345 }
346 }
347
348 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530349 public BgpPeerCfg.State getPeerConnState(String routerid) {
350 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530351
352 if (lspeer != null) {
353 return lspeer.getState();
354 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530355 return BgpPeerCfg.State.INVALID; //No instance
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530356 }
357 }
358
359 @Override
360 public boolean isPeerConnectable(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530361 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530362
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530363 if ((lspeer != null) && lspeer.getState().equals(BgpPeerCfg.State.IDLE)) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530364 return true;
365 }
366
367 return false;
368 }
369
370 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530371 public TreeMap<String, BgpPeerCfg> getPeerTree() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530372 return this.bgpPeerTree;
373 }
374
375 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530376 public TreeMap<String, BgpPeerCfg> displayPeers() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530377 if (this.bgpPeerTree.isEmpty()) {
378 log.debug("There are no BGP peers");
379 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530380 Set<Entry<String, BgpPeerCfg>> set = this.bgpPeerTree.entrySet();
381 Iterator<Entry<String, BgpPeerCfg>> list = set.iterator();
382 BgpPeerCfg lspeer;
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530383
384 while (list.hasNext()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530385 Entry<String, BgpPeerCfg> me = list.next();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530386 lspeer = me.getValue();
387 log.debug("Peer neighbor IP :" + me.getKey());
388 log.debug(", AS Number : " + lspeer.getAsNumber());
389 log.debug(", Hold Timer : " + lspeer.getHoldtime());
390 log.debug(", Is iBGP : " + lspeer.getIsIBgp());
391 }
392 }
393 return null;
394 }
395
396 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530397 public BgpPeerCfg displayPeers(String routerid) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530398
399 if (this.bgpPeerTree.isEmpty()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530400 log.debug("There are no Bgp peers");
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530401 } else {
402 return this.bgpPeerTree.get(routerid);
403 }
404 return null;
405 }
406
407 @Override
408 public void setHoldTime(short holdTime) {
409 this.holdTime = holdTime;
410 }
411
412 @Override
413 public short getHoldTime() {
414 return this.holdTime;
415 }
416
417 @Override
418 public boolean getLargeASCapability() {
419 return this.largeAs;
420 }
421
422 @Override
423 public void setLargeASCapability(boolean largeAs) {
424 this.largeAs = largeAs;
425 }
426
427 @Override
428 public boolean isPeerConfigured(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530429 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530430 return (lspeer != null) ? true : false;
431 }
432
433 @Override
434 public boolean isPeerConnected(String routerid) {
435 // TODO: is peer connected
436 return true;
437 }
438
439 @Override
440 public int getMaxConnRetryCount() {
441 return this.maxConnRetryCount;
442 }
443
444 @Override
445 public void setMaxConnRetryCout(int retryCount) {
446 this.maxConnRetryCount = retryCount;
447 }
448
449 @Override
450 public int getMaxConnRetryTime() {
451 return this.maxConnRetryTime;
452 }
453
454 @Override
455 public void setMaxConnRetryTime(int retryTime) {
456 this.maxConnRetryTime = retryTime;
457 }
458}