blob: 1c846eb61cae91adc5f6ef08282911fb4452c5b0 [file] [log] [blame]
Thejaswi N K6a4cd002015-09-21 17:19:55 +05301/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.bgp.controller.impl;
17
18import java.util.Iterator;
19import java.util.Map.Entry;
20import java.util.Set;
21import java.util.TreeMap;
22
23import org.onlab.packet.Ip4Address;
Shashikanth VH3fe37982015-11-30 11:50:07 +053024import org.onlab.packet.IpAddress;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053025import org.onosproject.bgp.controller.BgpCfg;
Shashikanth VH3fe37982015-11-30 11:50:07 +053026import org.onosproject.bgp.controller.BgpConnectPeer;
27import org.onosproject.bgp.controller.BgpController;
28import org.onosproject.bgp.controller.BgpId;
29import org.onosproject.bgp.controller.BgpPeer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053030import org.onosproject.bgp.controller.BgpPeerCfg;
Shashikanth VH3fe37982015-11-30 11:50:07 +053031import org.onosproject.bgp.controller.impl.BgpControllerImpl.BgpPeerManagerImpl;
Thejaswi N K6a4cd002015-09-21 17:19:55 +053032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35/**
36 * Provides BGP configuration of this BGP speaker.
37 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053038public class BgpConfig implements BgpCfg {
Thejaswi N K6a4cd002015-09-21 17:19:55 +053039
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053040 protected static final Logger log = LoggerFactory.getLogger(BgpConfig.class);
Thejaswi N K6a4cd002015-09-21 17:19:55 +053041
42 private static final short DEFAULT_HOLD_TIMER = 120;
43 private static final short DEFAULT_CONN_RETRY_TIME = 120;
44 private static final short DEFAULT_CONN_RETRY_COUNT = 5;
45
46 private State state = State.INIT;
47 private int localAs;
48 private int maxSession;
49 private boolean lsCapability;
50 private short holdTime;
51 private boolean largeAs = false;
52 private int maxConnRetryTime;
53 private int maxConnRetryCount;
54
55 private Ip4Address routerId = null;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053056 private TreeMap<String, BgpPeerCfg> bgpPeerTree = new TreeMap<>();
Shashikanth VH3fe37982015-11-30 11:50:07 +053057 private BgpConnectPeer connectPeer;
58 private BgpPeerManagerImpl peerManager;
59 private BgpController bgpController;
Thejaswi N K6a4cd002015-09-21 17:19:55 +053060
Shashikanth VH3fe37982015-11-30 11:50:07 +053061 /*
Thejaswi N K6a4cd002015-09-21 17:19:55 +053062 * Constructor to initialize the values.
63 */
Shashikanth VH3fe37982015-11-30 11:50:07 +053064 public BgpConfig(BgpController bgpController) {
65 this.bgpController = bgpController;
66 this.peerManager = (BgpPeerManagerImpl) bgpController.peerManager();
Thejaswi N K6a4cd002015-09-21 17:19:55 +053067 this.holdTime = DEFAULT_HOLD_TIMER;
68 this.maxConnRetryTime = DEFAULT_CONN_RETRY_TIME;
69 this.maxConnRetryCount = DEFAULT_CONN_RETRY_COUNT;
70 }
71
72 @Override
73 public State getState() {
74 return state;
75 }
76
77 @Override
78 public void setState(State state) {
79 this.state = state;
80 }
81
82 @Override
83 public int getAsNumber() {
84 return this.localAs;
85 }
86
87 @Override
88 public void setAsNumber(int localAs) {
89
90 State localState = getState();
91 this.localAs = localAs;
92
93 /* Set configuration state */
94 if (localState == State.IP_CONFIGURED) {
95 setState(State.IP_AS_CONFIGURED);
96 } else {
97 setState(State.AS_CONFIGURED);
98 }
99 }
100
101 @Override
102 public int getMaxSession() {
103 return this.maxSession;
104 }
105
106 @Override
107 public void setMaxSession(int maxSession) {
108 this.maxSession = maxSession;
109 }
110
111 @Override
112 public boolean getLsCapability() {
113 return this.lsCapability;
114 }
115
116 @Override
117 public void setLsCapability(boolean lsCapability) {
118 this.lsCapability = lsCapability;
119 }
120
121 @Override
122 public String getRouterId() {
123 if (this.routerId != null) {
124 return this.routerId.toString();
125 } else {
126 return null;
127 }
128 }
129
130 @Override
131 public void setRouterId(String routerId) {
132 State localState = getState();
133 this.routerId = Ip4Address.valueOf(routerId);
134
135 /* Set configuration state */
136 if (localState == State.AS_CONFIGURED) {
137 setState(State.IP_AS_CONFIGURED);
138 } else {
139 setState(State.IP_CONFIGURED);
140 }
141 }
142
143 @Override
144 public boolean addPeer(String routerid, int remoteAs) {
145 return addPeer(routerid, remoteAs, DEFAULT_HOLD_TIMER);
146 }
147
148 @Override
149 public boolean addPeer(String routerid, short holdTime) {
150 return addPeer(routerid, this.getAsNumber(), holdTime);
151 }
152
153 @Override
154 public boolean addPeer(String routerid, int remoteAs, short holdTime) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530155 BgpPeerConfig lspeer = new BgpPeerConfig();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530156 if (this.bgpPeerTree.get(routerid) == null) {
157
158 lspeer.setPeerRouterId(routerid);
159 lspeer.setAsNumber(remoteAs);
160 lspeer.setHoldtime(holdTime);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530161 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530162 lspeer.setSelfInnitConnection(false);
163
164 if (this.getAsNumber() == remoteAs) {
165 lspeer.setIsIBgp(true);
166 } else {
167 lspeer.setIsIBgp(false);
168 }
169
170 this.bgpPeerTree.put(routerid, lspeer);
171 log.debug("added successfully");
172 return true;
173 } else {
174 log.debug("already exists");
175 return false;
176 }
177 }
178
179 @Override
180 public boolean connectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530181 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530182
183 if (lspeer != null) {
184 lspeer.setSelfInnitConnection(true);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530185
186 if (lspeer.connectPeer() == null) {
187 connectPeer = new BgpConnectPeerImpl(bgpController, routerid, Controller.getBgpPortNum());
188 lspeer.setConnectPeer(connectPeer);
189 connectPeer.connectPeer();
190 }
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530191 return true;
192 }
193
194 return false;
195 }
196
197 @Override
198 public boolean removePeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530199 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530200
201 if (lspeer != null) {
202
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530203 disconnectPeer(routerid);
204 lspeer.setSelfInnitConnection(false);
205 lspeer = this.bgpPeerTree.remove(routerid);
206 log.debug("Deleted : " + routerid + " successfully");
207
208 return true;
209 } else {
210 log.debug("Did not find : " + routerid);
211 return false;
212 }
213 }
214
215 @Override
216 public boolean disconnectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530217 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530218
219 if (lspeer != null) {
220
Shashikanth VH3fe37982015-11-30 11:50:07 +0530221 BgpPeer disconnPeer = peerManager.getPeer(BgpId.bgpId(IpAddress.valueOf(routerid)));
222 if (disconnPeer != null) {
223 // TODO: send notification peer deconfigured
224 disconnPeer.disconnectPeer();
Shashikanth VH97e571e2016-01-05 12:15:14 +0530225 } else {
226 lspeer.connectPeer().disconnectPeer();
Shashikanth VH3fe37982015-11-30 11:50:07 +0530227 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530228 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530229 lspeer.setSelfInnitConnection(false);
230 log.debug("Disconnected : " + routerid + " successfully");
231
232 return true;
233 } else {
234 log.debug("Did not find : " + routerid);
235 return false;
236 }
237 }
238
239 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530240 public void setPeerConnState(String routerid, BgpPeerCfg.State state) {
241 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530242
243 if (lspeer != null) {
244 lspeer.setState(state);
245 log.debug("Peer : " + routerid + " is not available");
246
247 return;
248 } else {
249 log.debug("Did not find : " + routerid);
250 return;
251 }
252 }
253
254 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530255 public BgpPeerCfg.State getPeerConnState(String routerid) {
256 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530257
258 if (lspeer != null) {
259 return lspeer.getState();
260 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530261 return BgpPeerCfg.State.INVALID; //No instance
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530262 }
263 }
264
265 @Override
266 public boolean isPeerConnectable(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530267 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530268
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530269 if ((lspeer != null) && lspeer.getState().equals(BgpPeerCfg.State.IDLE)) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530270 return true;
271 }
272
273 return false;
274 }
275
276 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530277 public TreeMap<String, BgpPeerCfg> getPeerTree() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530278 return this.bgpPeerTree;
279 }
280
281 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530282 public TreeMap<String, BgpPeerCfg> displayPeers() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530283 if (this.bgpPeerTree.isEmpty()) {
284 log.debug("There are no BGP peers");
285 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530286 Set<Entry<String, BgpPeerCfg>> set = this.bgpPeerTree.entrySet();
287 Iterator<Entry<String, BgpPeerCfg>> list = set.iterator();
288 BgpPeerCfg lspeer;
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530289
290 while (list.hasNext()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530291 Entry<String, BgpPeerCfg> me = list.next();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530292 lspeer = me.getValue();
293 log.debug("Peer neighbor IP :" + me.getKey());
294 log.debug(", AS Number : " + lspeer.getAsNumber());
295 log.debug(", Hold Timer : " + lspeer.getHoldtime());
296 log.debug(", Is iBGP : " + lspeer.getIsIBgp());
297 }
298 }
299 return null;
300 }
301
302 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530303 public BgpPeerCfg displayPeers(String routerid) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530304
305 if (this.bgpPeerTree.isEmpty()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530306 log.debug("There are no Bgp peers");
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530307 } else {
308 return this.bgpPeerTree.get(routerid);
309 }
310 return null;
311 }
312
313 @Override
314 public void setHoldTime(short holdTime) {
315 this.holdTime = holdTime;
316 }
317
318 @Override
319 public short getHoldTime() {
320 return this.holdTime;
321 }
322
323 @Override
324 public boolean getLargeASCapability() {
325 return this.largeAs;
326 }
327
328 @Override
329 public void setLargeASCapability(boolean largeAs) {
330 this.largeAs = largeAs;
331 }
332
333 @Override
334 public boolean isPeerConfigured(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530335 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530336 return (lspeer != null) ? true : false;
337 }
338
339 @Override
340 public boolean isPeerConnected(String routerid) {
341 // TODO: is peer connected
342 return true;
343 }
344
345 @Override
346 public int getMaxConnRetryCount() {
347 return this.maxConnRetryCount;
348 }
349
350 @Override
351 public void setMaxConnRetryCout(int retryCount) {
352 this.maxConnRetryCount = retryCount;
353 }
354
355 @Override
356 public int getMaxConnRetryTime() {
357 return this.maxConnRetryTime;
358 }
359
360 @Override
361 public void setMaxConnRetryTime(int retryTime) {
362 this.maxConnRetryTime = retryTime;
363 }
364}