blob: 95a07ad508c2b2ec37b689d06275e080d40dd028 [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;
Shashikanth VHcea18d32015-12-04 15:45:14 +053070 this.lsCapability = true;
Thejaswi N K6a4cd002015-09-21 17:19:55 +053071 }
72
73 @Override
74 public State getState() {
75 return state;
76 }
77
78 @Override
79 public void setState(State state) {
80 this.state = state;
81 }
82
83 @Override
84 public int getAsNumber() {
85 return this.localAs;
86 }
87
88 @Override
89 public void setAsNumber(int localAs) {
90
91 State localState = getState();
92 this.localAs = localAs;
93
94 /* Set configuration state */
95 if (localState == State.IP_CONFIGURED) {
96 setState(State.IP_AS_CONFIGURED);
97 } else {
98 setState(State.AS_CONFIGURED);
99 }
100 }
101
102 @Override
103 public int getMaxSession() {
104 return this.maxSession;
105 }
106
107 @Override
108 public void setMaxSession(int maxSession) {
109 this.maxSession = maxSession;
110 }
111
112 @Override
113 public boolean getLsCapability() {
114 return this.lsCapability;
115 }
116
117 @Override
118 public void setLsCapability(boolean lsCapability) {
119 this.lsCapability = lsCapability;
120 }
121
122 @Override
123 public String getRouterId() {
124 if (this.routerId != null) {
125 return this.routerId.toString();
126 } else {
127 return null;
128 }
129 }
130
131 @Override
132 public void setRouterId(String routerId) {
133 State localState = getState();
134 this.routerId = Ip4Address.valueOf(routerId);
135
136 /* Set configuration state */
137 if (localState == State.AS_CONFIGURED) {
138 setState(State.IP_AS_CONFIGURED);
139 } else {
140 setState(State.IP_CONFIGURED);
141 }
142 }
143
144 @Override
145 public boolean addPeer(String routerid, int remoteAs) {
146 return addPeer(routerid, remoteAs, DEFAULT_HOLD_TIMER);
147 }
148
149 @Override
150 public boolean addPeer(String routerid, short holdTime) {
151 return addPeer(routerid, this.getAsNumber(), holdTime);
152 }
153
154 @Override
155 public boolean addPeer(String routerid, int remoteAs, short holdTime) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530156 BgpPeerConfig lspeer = new BgpPeerConfig();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530157 if (this.bgpPeerTree.get(routerid) == null) {
158
159 lspeer.setPeerRouterId(routerid);
160 lspeer.setAsNumber(remoteAs);
161 lspeer.setHoldtime(holdTime);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530162 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530163 lspeer.setSelfInnitConnection(false);
164
165 if (this.getAsNumber() == remoteAs) {
166 lspeer.setIsIBgp(true);
167 } else {
168 lspeer.setIsIBgp(false);
169 }
170
171 this.bgpPeerTree.put(routerid, lspeer);
172 log.debug("added successfully");
173 return true;
174 } else {
175 log.debug("already exists");
176 return false;
177 }
178 }
179
180 @Override
181 public boolean connectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530182 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530183
184 if (lspeer != null) {
185 lspeer.setSelfInnitConnection(true);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530186
187 if (lspeer.connectPeer() == null) {
188 connectPeer = new BgpConnectPeerImpl(bgpController, routerid, Controller.getBgpPortNum());
189 lspeer.setConnectPeer(connectPeer);
190 connectPeer.connectPeer();
191 }
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530192 return true;
193 }
194
195 return false;
196 }
197
198 @Override
199 public boolean removePeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530200 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530201
202 if (lspeer != null) {
203
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530204 disconnectPeer(routerid);
205 lspeer.setSelfInnitConnection(false);
206 lspeer = this.bgpPeerTree.remove(routerid);
207 log.debug("Deleted : " + routerid + " successfully");
208
209 return true;
210 } else {
211 log.debug("Did not find : " + routerid);
212 return false;
213 }
214 }
215
216 @Override
217 public boolean disconnectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530218 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530219
220 if (lspeer != null) {
221
Shashikanth VH3fe37982015-11-30 11:50:07 +0530222 BgpPeer disconnPeer = peerManager.getPeer(BgpId.bgpId(IpAddress.valueOf(routerid)));
223 if (disconnPeer != null) {
224 // TODO: send notification peer deconfigured
225 disconnPeer.disconnectPeer();
226 }
227 lspeer.connectPeer().disconnectPeer();
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}