blob: ac94822734b9203941bfd78dd879caf69e68a4af [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();
225 }
226 lspeer.connectPeer().disconnectPeer();
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530227 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530228 lspeer.setSelfInnitConnection(false);
229 log.debug("Disconnected : " + routerid + " successfully");
230
231 return true;
232 } else {
233 log.debug("Did not find : " + routerid);
234 return false;
235 }
236 }
237
238 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530239 public void setPeerConnState(String routerid, BgpPeerCfg.State state) {
240 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530241
242 if (lspeer != null) {
243 lspeer.setState(state);
244 log.debug("Peer : " + routerid + " is not available");
245
246 return;
247 } else {
248 log.debug("Did not find : " + routerid);
249 return;
250 }
251 }
252
253 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530254 public BgpPeerCfg.State getPeerConnState(String routerid) {
255 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530256
257 if (lspeer != null) {
258 return lspeer.getState();
259 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530260 return BgpPeerCfg.State.INVALID; //No instance
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530261 }
262 }
263
264 @Override
265 public boolean isPeerConnectable(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530266 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530267
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530268 if ((lspeer != null) && lspeer.getState().equals(BgpPeerCfg.State.IDLE)) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530269 return true;
270 }
271
272 return false;
273 }
274
275 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530276 public TreeMap<String, BgpPeerCfg> getPeerTree() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530277 return this.bgpPeerTree;
278 }
279
280 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530281 public TreeMap<String, BgpPeerCfg> displayPeers() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530282 if (this.bgpPeerTree.isEmpty()) {
283 log.debug("There are no BGP peers");
284 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530285 Set<Entry<String, BgpPeerCfg>> set = this.bgpPeerTree.entrySet();
286 Iterator<Entry<String, BgpPeerCfg>> list = set.iterator();
287 BgpPeerCfg lspeer;
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530288
289 while (list.hasNext()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530290 Entry<String, BgpPeerCfg> me = list.next();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530291 lspeer = me.getValue();
292 log.debug("Peer neighbor IP :" + me.getKey());
293 log.debug(", AS Number : " + lspeer.getAsNumber());
294 log.debug(", Hold Timer : " + lspeer.getHoldtime());
295 log.debug(", Is iBGP : " + lspeer.getIsIBgp());
296 }
297 }
298 return null;
299 }
300
301 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530302 public BgpPeerCfg displayPeers(String routerid) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530303
304 if (this.bgpPeerTree.isEmpty()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530305 log.debug("There are no Bgp peers");
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530306 } else {
307 return this.bgpPeerTree.get(routerid);
308 }
309 return null;
310 }
311
312 @Override
313 public void setHoldTime(short holdTime) {
314 this.holdTime = holdTime;
315 }
316
317 @Override
318 public short getHoldTime() {
319 return this.holdTime;
320 }
321
322 @Override
323 public boolean getLargeASCapability() {
324 return this.largeAs;
325 }
326
327 @Override
328 public void setLargeASCapability(boolean largeAs) {
329 this.largeAs = largeAs;
330 }
331
332 @Override
333 public boolean isPeerConfigured(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530334 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530335 return (lspeer != null) ? true : false;
336 }
337
338 @Override
339 public boolean isPeerConnected(String routerid) {
340 // TODO: is peer connected
341 return true;
342 }
343
344 @Override
345 public int getMaxConnRetryCount() {
346 return this.maxConnRetryCount;
347 }
348
349 @Override
350 public void setMaxConnRetryCout(int retryCount) {
351 this.maxConnRetryCount = retryCount;
352 }
353
354 @Override
355 public int getMaxConnRetryTime() {
356 return this.maxConnRetryTime;
357 }
358
359 @Override
360 public void setMaxConnRetryTime(int retryTime) {
361 this.maxConnRetryTime = retryTime;
362 }
363}