blob: bbeea485945fb902e38a380055d49c8ea666c1a9 [file] [log] [blame]
Thejaswi N K6a4cd002015-09-21 17:19:55 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
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;
Shashikanth VHb650bfa2016-04-18 12:54:03 +053060 private boolean rpdCapability;
Thejaswi N K6a4cd002015-09-21 17:19:55 +053061
Shashikanth VH3fe37982015-11-30 11:50:07 +053062 /*
Thejaswi N K6a4cd002015-09-21 17:19:55 +053063 * Constructor to initialize the values.
64 */
Shashikanth VH3fe37982015-11-30 11:50:07 +053065 public BgpConfig(BgpController bgpController) {
66 this.bgpController = bgpController;
67 this.peerManager = (BgpPeerManagerImpl) bgpController.peerManager();
Thejaswi N K6a4cd002015-09-21 17:19:55 +053068 this.holdTime = DEFAULT_HOLD_TIMER;
69 this.maxConnRetryTime = DEFAULT_CONN_RETRY_TIME;
70 this.maxConnRetryCount = DEFAULT_CONN_RETRY_COUNT;
71 }
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
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530123 public FlowSpec flowSpecCapability() {
124 return this.flowSpec;
125 }
126
127 @Override
128 public void setFlowSpecCapability(FlowSpec flowSpec) {
129 this.flowSpec = flowSpec;
130 }
131
132 @Override
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530133 public boolean flowSpecRpdCapability() {
134 return this.rpdCapability;
135 }
136
137 @Override
138 public void setFlowSpecRpdCapability(boolean rpdCapability) {
139 this.rpdCapability = rpdCapability;
140 }
141
142 @Override
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530143 public String getRouterId() {
144 if (this.routerId != null) {
145 return this.routerId.toString();
146 } else {
147 return null;
148 }
149 }
150
151 @Override
152 public void setRouterId(String routerId) {
153 State localState = getState();
154 this.routerId = Ip4Address.valueOf(routerId);
155
156 /* Set configuration state */
157 if (localState == State.AS_CONFIGURED) {
158 setState(State.IP_AS_CONFIGURED);
159 } else {
160 setState(State.IP_CONFIGURED);
161 }
162 }
163
164 @Override
165 public boolean addPeer(String routerid, int remoteAs) {
166 return addPeer(routerid, remoteAs, DEFAULT_HOLD_TIMER);
167 }
168
169 @Override
170 public boolean addPeer(String routerid, short holdTime) {
171 return addPeer(routerid, this.getAsNumber(), holdTime);
172 }
173
174 @Override
175 public boolean addPeer(String routerid, int remoteAs, short holdTime) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530176 BgpPeerConfig lspeer = new BgpPeerConfig();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530177 if (this.bgpPeerTree.get(routerid) == null) {
178
179 lspeer.setPeerRouterId(routerid);
180 lspeer.setAsNumber(remoteAs);
181 lspeer.setHoldtime(holdTime);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530182 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530183 lspeer.setSelfInnitConnection(false);
184
185 if (this.getAsNumber() == remoteAs) {
186 lspeer.setIsIBgp(true);
187 } else {
188 lspeer.setIsIBgp(false);
189 }
190
191 this.bgpPeerTree.put(routerid, lspeer);
192 log.debug("added successfully");
193 return true;
194 } else {
195 log.debug("already exists");
196 return false;
197 }
198 }
199
200 @Override
201 public boolean connectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530202 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530203
204 if (lspeer != null) {
205 lspeer.setSelfInnitConnection(true);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530206
207 if (lspeer.connectPeer() == null) {
208 connectPeer = new BgpConnectPeerImpl(bgpController, routerid, Controller.getBgpPortNum());
209 lspeer.setConnectPeer(connectPeer);
210 connectPeer.connectPeer();
211 }
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530212 return true;
213 }
214
215 return false;
216 }
217
218 @Override
219 public boolean removePeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530220 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530221
222 if (lspeer != null) {
223
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530224 disconnectPeer(routerid);
225 lspeer.setSelfInnitConnection(false);
226 lspeer = this.bgpPeerTree.remove(routerid);
227 log.debug("Deleted : " + routerid + " successfully");
228
229 return true;
230 } else {
231 log.debug("Did not find : " + routerid);
232 return false;
233 }
234 }
235
236 @Override
237 public boolean disconnectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530238 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530239
240 if (lspeer != null) {
241
Shashikanth VH3fe37982015-11-30 11:50:07 +0530242 BgpPeer disconnPeer = peerManager.getPeer(BgpId.bgpId(IpAddress.valueOf(routerid)));
243 if (disconnPeer != null) {
244 // TODO: send notification peer deconfigured
245 disconnPeer.disconnectPeer();
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530246 } else if (lspeer.connectPeer() != null) {
Shashikanth VH97e571e2016-01-05 12:15:14 +0530247 lspeer.connectPeer().disconnectPeer();
Shashikanth VH3fe37982015-11-30 11:50:07 +0530248 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530249 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530250 lspeer.setSelfInnitConnection(false);
251 log.debug("Disconnected : " + routerid + " successfully");
252
253 return true;
254 } else {
255 log.debug("Did not find : " + routerid);
256 return false;
257 }
258 }
259
260 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530261 public void setPeerConnState(String routerid, BgpPeerCfg.State state) {
262 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530263
264 if (lspeer != null) {
265 lspeer.setState(state);
266 log.debug("Peer : " + routerid + " is not available");
267
268 return;
269 } else {
270 log.debug("Did not find : " + routerid);
271 return;
272 }
273 }
274
275 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530276 public BgpPeerCfg.State getPeerConnState(String routerid) {
277 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530278
279 if (lspeer != null) {
280 return lspeer.getState();
281 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530282 return BgpPeerCfg.State.INVALID; //No instance
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530283 }
284 }
285
286 @Override
287 public boolean isPeerConnectable(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530288 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530289
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530290 if ((lspeer != null) && lspeer.getState().equals(BgpPeerCfg.State.IDLE)) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530291 return true;
292 }
293
294 return false;
295 }
296
297 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530298 public TreeMap<String, BgpPeerCfg> getPeerTree() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530299 return this.bgpPeerTree;
300 }
301
302 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530303 public TreeMap<String, BgpPeerCfg> displayPeers() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530304 if (this.bgpPeerTree.isEmpty()) {
305 log.debug("There are no BGP peers");
306 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530307 Set<Entry<String, BgpPeerCfg>> set = this.bgpPeerTree.entrySet();
308 Iterator<Entry<String, BgpPeerCfg>> list = set.iterator();
309 BgpPeerCfg lspeer;
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530310
311 while (list.hasNext()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530312 Entry<String, BgpPeerCfg> me = list.next();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530313 lspeer = me.getValue();
314 log.debug("Peer neighbor IP :" + me.getKey());
315 log.debug(", AS Number : " + lspeer.getAsNumber());
316 log.debug(", Hold Timer : " + lspeer.getHoldtime());
317 log.debug(", Is iBGP : " + lspeer.getIsIBgp());
318 }
319 }
320 return null;
321 }
322
323 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530324 public BgpPeerCfg displayPeers(String routerid) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530325
326 if (this.bgpPeerTree.isEmpty()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530327 log.debug("There are no Bgp peers");
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530328 } else {
329 return this.bgpPeerTree.get(routerid);
330 }
331 return null;
332 }
333
334 @Override
335 public void setHoldTime(short holdTime) {
336 this.holdTime = holdTime;
337 }
338
339 @Override
340 public short getHoldTime() {
341 return this.holdTime;
342 }
343
344 @Override
345 public boolean getLargeASCapability() {
346 return this.largeAs;
347 }
348
349 @Override
350 public void setLargeASCapability(boolean largeAs) {
351 this.largeAs = largeAs;
352 }
353
354 @Override
355 public boolean isPeerConfigured(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530356 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530357 return (lspeer != null) ? true : false;
358 }
359
360 @Override
361 public boolean isPeerConnected(String routerid) {
362 // TODO: is peer connected
363 return true;
364 }
365
366 @Override
367 public int getMaxConnRetryCount() {
368 return this.maxConnRetryCount;
369 }
370
371 @Override
372 public void setMaxConnRetryCout(int retryCount) {
373 this.maxConnRetryCount = retryCount;
374 }
375
376 @Override
377 public int getMaxConnRetryTime() {
378 return this.maxConnRetryTime;
379 }
380
381 @Override
382 public void setMaxConnRetryTime(int retryTime) {
383 this.maxConnRetryTime = retryTime;
384 }
385}