blob: 5b367a74ba07c0e95c54cdc4146d0c4e1148eca1 [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;
Shashikanth VH580bdeb2016-02-19 17:26:03 +053054 private FlowSpec flowSpec = FlowSpec.NONE;
Thejaswi N K6a4cd002015-09-21 17:19:55 +053055 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
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530122 public FlowSpec flowSpecCapability() {
123 return this.flowSpec;
124 }
125
126 @Override
127 public void setFlowSpecCapability(FlowSpec flowSpec) {
128 this.flowSpec = flowSpec;
129 }
130
131 @Override
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530132 public String getRouterId() {
133 if (this.routerId != null) {
134 return this.routerId.toString();
135 } else {
136 return null;
137 }
138 }
139
140 @Override
141 public void setRouterId(String routerId) {
142 State localState = getState();
143 this.routerId = Ip4Address.valueOf(routerId);
144
145 /* Set configuration state */
146 if (localState == State.AS_CONFIGURED) {
147 setState(State.IP_AS_CONFIGURED);
148 } else {
149 setState(State.IP_CONFIGURED);
150 }
151 }
152
153 @Override
154 public boolean addPeer(String routerid, int remoteAs) {
155 return addPeer(routerid, remoteAs, DEFAULT_HOLD_TIMER);
156 }
157
158 @Override
159 public boolean addPeer(String routerid, short holdTime) {
160 return addPeer(routerid, this.getAsNumber(), holdTime);
161 }
162
163 @Override
164 public boolean addPeer(String routerid, int remoteAs, short holdTime) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530165 BgpPeerConfig lspeer = new BgpPeerConfig();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530166 if (this.bgpPeerTree.get(routerid) == null) {
167
168 lspeer.setPeerRouterId(routerid);
169 lspeer.setAsNumber(remoteAs);
170 lspeer.setHoldtime(holdTime);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530171 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530172 lspeer.setSelfInnitConnection(false);
173
174 if (this.getAsNumber() == remoteAs) {
175 lspeer.setIsIBgp(true);
176 } else {
177 lspeer.setIsIBgp(false);
178 }
179
180 this.bgpPeerTree.put(routerid, lspeer);
181 log.debug("added successfully");
182 return true;
183 } else {
184 log.debug("already exists");
185 return false;
186 }
187 }
188
189 @Override
190 public boolean connectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530191 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530192
193 if (lspeer != null) {
194 lspeer.setSelfInnitConnection(true);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530195
196 if (lspeer.connectPeer() == null) {
197 connectPeer = new BgpConnectPeerImpl(bgpController, routerid, Controller.getBgpPortNum());
198 lspeer.setConnectPeer(connectPeer);
199 connectPeer.connectPeer();
200 }
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530201 return true;
202 }
203
204 return false;
205 }
206
207 @Override
208 public boolean removePeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530209 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530210
211 if (lspeer != null) {
212
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530213 disconnectPeer(routerid);
214 lspeer.setSelfInnitConnection(false);
215 lspeer = this.bgpPeerTree.remove(routerid);
216 log.debug("Deleted : " + routerid + " successfully");
217
218 return true;
219 } else {
220 log.debug("Did not find : " + routerid);
221 return false;
222 }
223 }
224
225 @Override
226 public boolean disconnectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530227 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530228
229 if (lspeer != null) {
230
Shashikanth VH3fe37982015-11-30 11:50:07 +0530231 BgpPeer disconnPeer = peerManager.getPeer(BgpId.bgpId(IpAddress.valueOf(routerid)));
232 if (disconnPeer != null) {
233 // TODO: send notification peer deconfigured
234 disconnPeer.disconnectPeer();
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530235 } else if (lspeer.connectPeer() != null) {
Shashikanth VH97e571e2016-01-05 12:15:14 +0530236 lspeer.connectPeer().disconnectPeer();
Shashikanth VH3fe37982015-11-30 11:50:07 +0530237 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530238 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530239 lspeer.setSelfInnitConnection(false);
240 log.debug("Disconnected : " + routerid + " successfully");
241
242 return true;
243 } else {
244 log.debug("Did not find : " + routerid);
245 return false;
246 }
247 }
248
249 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530250 public void setPeerConnState(String routerid, BgpPeerCfg.State state) {
251 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530252
253 if (lspeer != null) {
254 lspeer.setState(state);
255 log.debug("Peer : " + routerid + " is not available");
256
257 return;
258 } else {
259 log.debug("Did not find : " + routerid);
260 return;
261 }
262 }
263
264 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530265 public BgpPeerCfg.State getPeerConnState(String routerid) {
266 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530267
268 if (lspeer != null) {
269 return lspeer.getState();
270 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530271 return BgpPeerCfg.State.INVALID; //No instance
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530272 }
273 }
274
275 @Override
276 public boolean isPeerConnectable(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530277 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530278
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530279 if ((lspeer != null) && lspeer.getState().equals(BgpPeerCfg.State.IDLE)) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530280 return true;
281 }
282
283 return false;
284 }
285
286 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530287 public TreeMap<String, BgpPeerCfg> getPeerTree() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530288 return this.bgpPeerTree;
289 }
290
291 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530292 public TreeMap<String, BgpPeerCfg> displayPeers() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530293 if (this.bgpPeerTree.isEmpty()) {
294 log.debug("There are no BGP peers");
295 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530296 Set<Entry<String, BgpPeerCfg>> set = this.bgpPeerTree.entrySet();
297 Iterator<Entry<String, BgpPeerCfg>> list = set.iterator();
298 BgpPeerCfg lspeer;
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530299
300 while (list.hasNext()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530301 Entry<String, BgpPeerCfg> me = list.next();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530302 lspeer = me.getValue();
303 log.debug("Peer neighbor IP :" + me.getKey());
304 log.debug(", AS Number : " + lspeer.getAsNumber());
305 log.debug(", Hold Timer : " + lspeer.getHoldtime());
306 log.debug(", Is iBGP : " + lspeer.getIsIBgp());
307 }
308 }
309 return null;
310 }
311
312 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530313 public BgpPeerCfg displayPeers(String routerid) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530314
315 if (this.bgpPeerTree.isEmpty()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530316 log.debug("There are no Bgp peers");
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530317 } else {
318 return this.bgpPeerTree.get(routerid);
319 }
320 return null;
321 }
322
323 @Override
324 public void setHoldTime(short holdTime) {
325 this.holdTime = holdTime;
326 }
327
328 @Override
329 public short getHoldTime() {
330 return this.holdTime;
331 }
332
333 @Override
334 public boolean getLargeASCapability() {
335 return this.largeAs;
336 }
337
338 @Override
339 public void setLargeASCapability(boolean largeAs) {
340 this.largeAs = largeAs;
341 }
342
343 @Override
344 public boolean isPeerConfigured(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530345 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530346 return (lspeer != null) ? true : false;
347 }
348
349 @Override
350 public boolean isPeerConnected(String routerid) {
351 // TODO: is peer connected
352 return true;
353 }
354
355 @Override
356 public int getMaxConnRetryCount() {
357 return this.maxConnRetryCount;
358 }
359
360 @Override
361 public void setMaxConnRetryCout(int retryCount) {
362 this.maxConnRetryCount = retryCount;
363 }
364
365 @Override
366 public int getMaxConnRetryTime() {
367 return this.maxConnRetryTime;
368 }
369
370 @Override
371 public void setMaxConnRetryTime(int retryTime) {
372 this.maxConnRetryTime = retryTime;
373 }
374}