blob: e6caee76e8269709719c4501442bd18197ad8e67 [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
Shashikanth VH3fe37982015-11-30 11:50:07 +053065 /*
Thejaswi N K6a4cd002015-09-21 17:19:55 +053066 * Constructor to initialize the values.
67 */
Shashikanth VH3fe37982015-11-30 11:50:07 +053068 public BgpConfig(BgpController bgpController) {
69 this.bgpController = bgpController;
70 this.peerManager = (BgpPeerManagerImpl) bgpController.peerManager();
Thejaswi N K6a4cd002015-09-21 17:19:55 +053071 this.holdTime = DEFAULT_HOLD_TIMER;
72 this.maxConnRetryTime = DEFAULT_CONN_RETRY_TIME;
73 this.maxConnRetryCount = DEFAULT_CONN_RETRY_COUNT;
74 }
75
76 @Override
77 public State getState() {
78 return state;
79 }
80
81 @Override
82 public void setState(State state) {
83 this.state = state;
84 }
85
86 @Override
87 public int getAsNumber() {
88 return this.localAs;
89 }
90
91 @Override
92 public void setAsNumber(int localAs) {
93
94 State localState = getState();
95 this.localAs = localAs;
96
97 /* Set configuration state */
98 if (localState == State.IP_CONFIGURED) {
99 setState(State.IP_AS_CONFIGURED);
100 } else {
101 setState(State.AS_CONFIGURED);
102 }
103 }
104
105 @Override
106 public int getMaxSession() {
107 return this.maxSession;
108 }
109
110 @Override
111 public void setMaxSession(int maxSession) {
112 this.maxSession = maxSession;
113 }
114
115 @Override
116 public boolean getLsCapability() {
117 return this.lsCapability;
118 }
119
120 @Override
121 public void setLsCapability(boolean lsCapability) {
122 this.lsCapability = lsCapability;
123 }
124
125 @Override
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530126 public FlowSpec flowSpecCapability() {
127 return this.flowSpec;
128 }
129
130 @Override
131 public void setFlowSpecCapability(FlowSpec flowSpec) {
132 this.flowSpec = flowSpec;
133 }
134
135 @Override
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530136 public boolean flowSpecRpdCapability() {
137 return this.rpdCapability;
138 }
139
140 @Override
141 public void setFlowSpecRpdCapability(boolean rpdCapability) {
142 this.rpdCapability = rpdCapability;
143 }
144
145 @Override
Mohammad Shahid30fedc52017-08-09 11:49:40 +0530146 public boolean getEvpnCapability() {
147 return this.evpnCapability;
148 }
149
150 @Override
151 public void setEvpnCapability(boolean evpnCapability) {
152 this.evpnCapability = evpnCapability;
153 }
154
155 @Override
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530156 public String getRouterId() {
157 if (this.routerId != null) {
158 return this.routerId.toString();
159 } else {
160 return null;
161 }
162 }
163
164 @Override
165 public void setRouterId(String routerId) {
166 State localState = getState();
167 this.routerId = Ip4Address.valueOf(routerId);
168
169 /* Set configuration state */
170 if (localState == State.AS_CONFIGURED) {
171 setState(State.IP_AS_CONFIGURED);
172 } else {
173 setState(State.IP_CONFIGURED);
174 }
175 }
176
177 @Override
178 public boolean addPeer(String routerid, int remoteAs) {
179 return addPeer(routerid, remoteAs, DEFAULT_HOLD_TIMER);
180 }
181
182 @Override
183 public boolean addPeer(String routerid, short holdTime) {
184 return addPeer(routerid, this.getAsNumber(), holdTime);
185 }
186
187 @Override
188 public boolean addPeer(String routerid, int remoteAs, short holdTime) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530189 BgpPeerConfig lspeer = new BgpPeerConfig();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530190 if (this.bgpPeerTree.get(routerid) == null) {
191
192 lspeer.setPeerRouterId(routerid);
193 lspeer.setAsNumber(remoteAs);
194 lspeer.setHoldtime(holdTime);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530195 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530196 lspeer.setSelfInnitConnection(false);
197
198 if (this.getAsNumber() == remoteAs) {
199 lspeer.setIsIBgp(true);
200 } else {
201 lspeer.setIsIBgp(false);
202 }
203
204 this.bgpPeerTree.put(routerid, lspeer);
mohamedrahil00f6f262016-11-24 20:20:41 +0530205 log.debug("Added successfully");
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530206 return true;
207 } else {
mohamedrahil00f6f262016-11-24 20:20:41 +0530208 log.debug("Already exists");
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530209 return false;
210 }
211 }
212
213 @Override
214 public boolean connectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530215 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530216
217 if (lspeer != null) {
218 lspeer.setSelfInnitConnection(true);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530219
220 if (lspeer.connectPeer() == null) {
Shashikanth VH83f77e42016-05-26 20:34:56 +0530221 connectPeer = new BgpConnectPeerImpl(bgpController, routerid, Controller.BGP_PORT_NUM);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530222 lspeer.setConnectPeer(connectPeer);
223 connectPeer.connectPeer();
mohamedrahil00f6f262016-11-24 20:20:41 +0530224 peerList.add((BgpConnectPeerImpl) connectPeer);
225
Shashikanth VH3fe37982015-11-30 11:50:07 +0530226 }
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530227 return true;
228 }
229
230 return false;
231 }
232
233 @Override
234 public boolean removePeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530235 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530236
237 if (lspeer != null) {
238
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530239 disconnectPeer(routerid);
240 lspeer.setSelfInnitConnection(false);
241 lspeer = this.bgpPeerTree.remove(routerid);
242 log.debug("Deleted : " + routerid + " successfully");
243
244 return true;
245 } else {
246 log.debug("Did not find : " + routerid);
247 return false;
248 }
249 }
250
251 @Override
252 public boolean disconnectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530253 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530254
255 if (lspeer != null) {
256
Shashikanth VH3fe37982015-11-30 11:50:07 +0530257 BgpPeer disconnPeer = peerManager.getPeer(BgpId.bgpId(IpAddress.valueOf(routerid)));
258 if (disconnPeer != null) {
259 // TODO: send notification peer deconfigured
260 disconnPeer.disconnectPeer();
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530261 } else if (lspeer.connectPeer() != null) {
Shashikanth VH97e571e2016-01-05 12:15:14 +0530262 lspeer.connectPeer().disconnectPeer();
Shashikanth VH3fe37982015-11-30 11:50:07 +0530263 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530264 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530265 lspeer.setSelfInnitConnection(false);
266 log.debug("Disconnected : " + routerid + " successfully");
267
268 return true;
269 } else {
270 log.debug("Did not find : " + routerid);
271 return false;
272 }
273 }
274
275 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530276 public void setPeerConnState(String routerid, BgpPeerCfg.State state) {
277 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530278
279 if (lspeer != null) {
280 lspeer.setState(state);
281 log.debug("Peer : " + routerid + " is not available");
282
283 return;
284 } else {
285 log.debug("Did not find : " + routerid);
286 return;
287 }
288 }
289
290 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530291 public BgpPeerCfg.State getPeerConnState(String routerid) {
292 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530293
294 if (lspeer != null) {
295 return lspeer.getState();
296 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530297 return BgpPeerCfg.State.INVALID; //No instance
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530298 }
299 }
300
301 @Override
302 public boolean isPeerConnectable(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530303 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530304
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530305 if ((lspeer != null) && lspeer.getState().equals(BgpPeerCfg.State.IDLE)) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530306 return true;
307 }
308
309 return false;
310 }
311
312 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530313 public TreeMap<String, BgpPeerCfg> getPeerTree() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530314 return this.bgpPeerTree;
315 }
316
317 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530318 public TreeMap<String, BgpPeerCfg> displayPeers() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530319 if (this.bgpPeerTree.isEmpty()) {
320 log.debug("There are no BGP peers");
321 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530322 Set<Entry<String, BgpPeerCfg>> set = this.bgpPeerTree.entrySet();
323 Iterator<Entry<String, BgpPeerCfg>> list = set.iterator();
324 BgpPeerCfg lspeer;
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530325
326 while (list.hasNext()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530327 Entry<String, BgpPeerCfg> me = list.next();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530328 lspeer = me.getValue();
329 log.debug("Peer neighbor IP :" + me.getKey());
330 log.debug(", AS Number : " + lspeer.getAsNumber());
331 log.debug(", Hold Timer : " + lspeer.getHoldtime());
332 log.debug(", Is iBGP : " + lspeer.getIsIBgp());
333 }
334 }
335 return null;
336 }
337
338 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530339 public BgpPeerCfg displayPeers(String routerid) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530340
341 if (this.bgpPeerTree.isEmpty()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530342 log.debug("There are no Bgp peers");
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530343 } else {
344 return this.bgpPeerTree.get(routerid);
345 }
346 return null;
347 }
348
349 @Override
350 public void setHoldTime(short holdTime) {
351 this.holdTime = holdTime;
352 }
353
354 @Override
355 public short getHoldTime() {
356 return this.holdTime;
357 }
358
359 @Override
360 public boolean getLargeASCapability() {
361 return this.largeAs;
362 }
363
364 @Override
365 public void setLargeASCapability(boolean largeAs) {
366 this.largeAs = largeAs;
367 }
368
369 @Override
370 public boolean isPeerConfigured(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530371 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530372 return (lspeer != null) ? true : false;
373 }
374
375 @Override
376 public boolean isPeerConnected(String routerid) {
377 // TODO: is peer connected
378 return true;
379 }
380
381 @Override
382 public int getMaxConnRetryCount() {
383 return this.maxConnRetryCount;
384 }
385
386 @Override
387 public void setMaxConnRetryCout(int retryCount) {
388 this.maxConnRetryCount = retryCount;
389 }
390
391 @Override
392 public int getMaxConnRetryTime() {
393 return this.maxConnRetryTime;
394 }
395
396 @Override
397 public void setMaxConnRetryTime(int retryTime) {
398 this.maxConnRetryTime = retryTime;
399 }
400}