blob: 867f6a670ac5c308a3caa7c3ff75f72bf428f753 [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;
mohamedrahil00f6f262016-11-24 20:20:41 +053022import java.util.List;
23import java.util.ArrayList;
Thejaswi N K6a4cd002015-09-21 17:19:55 +053024
25import org.onlab.packet.Ip4Address;
Shashikanth VH3fe37982015-11-30 11:50:07 +053026import org.onlab.packet.IpAddress;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053027import org.onosproject.bgp.controller.BgpCfg;
Shashikanth VH3fe37982015-11-30 11:50:07 +053028import org.onosproject.bgp.controller.BgpConnectPeer;
29import org.onosproject.bgp.controller.BgpController;
30import org.onosproject.bgp.controller.BgpId;
31import org.onosproject.bgp.controller.BgpPeer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053032import org.onosproject.bgp.controller.BgpPeerCfg;
Shashikanth VH3fe37982015-11-30 11:50:07 +053033import org.onosproject.bgp.controller.impl.BgpControllerImpl.BgpPeerManagerImpl;
Thejaswi N K6a4cd002015-09-21 17:19:55 +053034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
37/**
38 * Provides BGP configuration of this BGP speaker.
39 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053040public class BgpConfig implements BgpCfg {
Thejaswi N K6a4cd002015-09-21 17:19:55 +053041
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053042 protected static final Logger log = LoggerFactory.getLogger(BgpConfig.class);
Thejaswi N K6a4cd002015-09-21 17:19:55 +053043
44 private static final short DEFAULT_HOLD_TIMER = 120;
45 private static final short DEFAULT_CONN_RETRY_TIME = 120;
46 private static final short DEFAULT_CONN_RETRY_COUNT = 5;
mohamedrahil00f6f262016-11-24 20:20:41 +053047 private List<BgpConnectPeerImpl> peerList = new ArrayList();
Thejaswi N K6a4cd002015-09-21 17:19:55 +053048 private State state = State.INIT;
49 private int localAs;
50 private int maxSession;
51 private boolean lsCapability;
52 private short holdTime;
53 private boolean largeAs = false;
54 private int maxConnRetryTime;
55 private int maxConnRetryCount;
Shashikanth VH580bdeb2016-02-19 17:26:03 +053056 private FlowSpec flowSpec = FlowSpec.NONE;
Thejaswi N K6a4cd002015-09-21 17:19:55 +053057 private Ip4Address routerId = null;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053058 private TreeMap<String, BgpPeerCfg> bgpPeerTree = new TreeMap<>();
Shashikanth VH3fe37982015-11-30 11:50:07 +053059 private BgpConnectPeer connectPeer;
60 private BgpPeerManagerImpl peerManager;
61 private BgpController bgpController;
Shashikanth VHb650bfa2016-04-18 12:54:03 +053062 private boolean rpdCapability;
Thejaswi N K6a4cd002015-09-21 17:19:55 +053063
Shashikanth VH3fe37982015-11-30 11:50:07 +053064 /*
Thejaswi N K6a4cd002015-09-21 17:19:55 +053065 * Constructor to initialize the values.
66 */
Shashikanth VH3fe37982015-11-30 11:50:07 +053067 public BgpConfig(BgpController bgpController) {
68 this.bgpController = bgpController;
69 this.peerManager = (BgpPeerManagerImpl) bgpController.peerManager();
Thejaswi N K6a4cd002015-09-21 17:19:55 +053070 this.holdTime = DEFAULT_HOLD_TIMER;
71 this.maxConnRetryTime = DEFAULT_CONN_RETRY_TIME;
72 this.maxConnRetryCount = DEFAULT_CONN_RETRY_COUNT;
73 }
74
75 @Override
76 public State getState() {
77 return state;
78 }
79
80 @Override
81 public void setState(State state) {
82 this.state = state;
83 }
84
85 @Override
86 public int getAsNumber() {
87 return this.localAs;
88 }
89
90 @Override
91 public void setAsNumber(int localAs) {
92
93 State localState = getState();
94 this.localAs = localAs;
95
96 /* Set configuration state */
97 if (localState == State.IP_CONFIGURED) {
98 setState(State.IP_AS_CONFIGURED);
99 } else {
100 setState(State.AS_CONFIGURED);
101 }
102 }
103
104 @Override
105 public int getMaxSession() {
106 return this.maxSession;
107 }
108
109 @Override
110 public void setMaxSession(int maxSession) {
111 this.maxSession = maxSession;
112 }
113
114 @Override
115 public boolean getLsCapability() {
116 return this.lsCapability;
117 }
118
119 @Override
120 public void setLsCapability(boolean lsCapability) {
121 this.lsCapability = lsCapability;
122 }
123
124 @Override
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530125 public FlowSpec flowSpecCapability() {
126 return this.flowSpec;
127 }
128
129 @Override
130 public void setFlowSpecCapability(FlowSpec flowSpec) {
131 this.flowSpec = flowSpec;
132 }
133
134 @Override
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530135 public boolean flowSpecRpdCapability() {
136 return this.rpdCapability;
137 }
138
139 @Override
140 public void setFlowSpecRpdCapability(boolean rpdCapability) {
141 this.rpdCapability = rpdCapability;
142 }
143
144 @Override
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530145 public String getRouterId() {
146 if (this.routerId != null) {
147 return this.routerId.toString();
148 } else {
149 return null;
150 }
151 }
152
153 @Override
154 public void setRouterId(String routerId) {
155 State localState = getState();
156 this.routerId = Ip4Address.valueOf(routerId);
157
158 /* Set configuration state */
159 if (localState == State.AS_CONFIGURED) {
160 setState(State.IP_AS_CONFIGURED);
161 } else {
162 setState(State.IP_CONFIGURED);
163 }
164 }
165
166 @Override
167 public boolean addPeer(String routerid, int remoteAs) {
168 return addPeer(routerid, remoteAs, DEFAULT_HOLD_TIMER);
169 }
170
171 @Override
172 public boolean addPeer(String routerid, short holdTime) {
173 return addPeer(routerid, this.getAsNumber(), holdTime);
174 }
175
176 @Override
177 public boolean addPeer(String routerid, int remoteAs, short holdTime) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530178 BgpPeerConfig lspeer = new BgpPeerConfig();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530179 if (this.bgpPeerTree.get(routerid) == null) {
180
181 lspeer.setPeerRouterId(routerid);
182 lspeer.setAsNumber(remoteAs);
183 lspeer.setHoldtime(holdTime);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530184 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530185 lspeer.setSelfInnitConnection(false);
186
187 if (this.getAsNumber() == remoteAs) {
188 lspeer.setIsIBgp(true);
189 } else {
190 lspeer.setIsIBgp(false);
191 }
192
193 this.bgpPeerTree.put(routerid, lspeer);
mohamedrahil00f6f262016-11-24 20:20:41 +0530194 log.debug("Added successfully");
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530195 return true;
196 } else {
mohamedrahil00f6f262016-11-24 20:20:41 +0530197 log.debug("Already exists");
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530198 return false;
199 }
200 }
201
202 @Override
203 public boolean connectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530204 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530205
206 if (lspeer != null) {
207 lspeer.setSelfInnitConnection(true);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530208
209 if (lspeer.connectPeer() == null) {
Shashikanth VH83f77e42016-05-26 20:34:56 +0530210 connectPeer = new BgpConnectPeerImpl(bgpController, routerid, Controller.BGP_PORT_NUM);
Shashikanth VH3fe37982015-11-30 11:50:07 +0530211 lspeer.setConnectPeer(connectPeer);
212 connectPeer.connectPeer();
mohamedrahil00f6f262016-11-24 20:20:41 +0530213 peerList.add((BgpConnectPeerImpl) connectPeer);
214
Shashikanth VH3fe37982015-11-30 11:50:07 +0530215 }
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530216 return true;
217 }
218
219 return false;
220 }
221
222 @Override
223 public boolean removePeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530224 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530225
226 if (lspeer != null) {
227
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530228 disconnectPeer(routerid);
229 lspeer.setSelfInnitConnection(false);
230 lspeer = this.bgpPeerTree.remove(routerid);
231 log.debug("Deleted : " + routerid + " successfully");
232
233 return true;
234 } else {
235 log.debug("Did not find : " + routerid);
236 return false;
237 }
238 }
239
240 @Override
241 public boolean disconnectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530242 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530243
244 if (lspeer != null) {
245
Shashikanth VH3fe37982015-11-30 11:50:07 +0530246 BgpPeer disconnPeer = peerManager.getPeer(BgpId.bgpId(IpAddress.valueOf(routerid)));
247 if (disconnPeer != null) {
248 // TODO: send notification peer deconfigured
249 disconnPeer.disconnectPeer();
Shashikanth VH580bdeb2016-02-19 17:26:03 +0530250 } else if (lspeer.connectPeer() != null) {
Shashikanth VH97e571e2016-01-05 12:15:14 +0530251 lspeer.connectPeer().disconnectPeer();
Shashikanth VH3fe37982015-11-30 11:50:07 +0530252 }
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530253 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530254 lspeer.setSelfInnitConnection(false);
255 log.debug("Disconnected : " + routerid + " successfully");
256
257 return true;
258 } else {
259 log.debug("Did not find : " + routerid);
260 return false;
261 }
262 }
263
264 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530265 public void setPeerConnState(String routerid, BgpPeerCfg.State state) {
266 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530267
268 if (lspeer != null) {
269 lspeer.setState(state);
270 log.debug("Peer : " + routerid + " is not available");
271
272 return;
273 } else {
274 log.debug("Did not find : " + routerid);
275 return;
276 }
277 }
278
279 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530280 public BgpPeerCfg.State getPeerConnState(String routerid) {
281 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530282
283 if (lspeer != null) {
284 return lspeer.getState();
285 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530286 return BgpPeerCfg.State.INVALID; //No instance
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530287 }
288 }
289
290 @Override
291 public boolean isPeerConnectable(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530292 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530293
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530294 if ((lspeer != null) && lspeer.getState().equals(BgpPeerCfg.State.IDLE)) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530295 return true;
296 }
297
298 return false;
299 }
300
301 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530302 public TreeMap<String, BgpPeerCfg> getPeerTree() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530303 return this.bgpPeerTree;
304 }
305
306 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530307 public TreeMap<String, BgpPeerCfg> displayPeers() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530308 if (this.bgpPeerTree.isEmpty()) {
309 log.debug("There are no BGP peers");
310 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530311 Set<Entry<String, BgpPeerCfg>> set = this.bgpPeerTree.entrySet();
312 Iterator<Entry<String, BgpPeerCfg>> list = set.iterator();
313 BgpPeerCfg lspeer;
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530314
315 while (list.hasNext()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530316 Entry<String, BgpPeerCfg> me = list.next();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530317 lspeer = me.getValue();
318 log.debug("Peer neighbor IP :" + me.getKey());
319 log.debug(", AS Number : " + lspeer.getAsNumber());
320 log.debug(", Hold Timer : " + lspeer.getHoldtime());
321 log.debug(", Is iBGP : " + lspeer.getIsIBgp());
322 }
323 }
324 return null;
325 }
326
327 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530328 public BgpPeerCfg displayPeers(String routerid) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530329
330 if (this.bgpPeerTree.isEmpty()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530331 log.debug("There are no Bgp peers");
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530332 } else {
333 return this.bgpPeerTree.get(routerid);
334 }
335 return null;
336 }
337
338 @Override
339 public void setHoldTime(short holdTime) {
340 this.holdTime = holdTime;
341 }
342
343 @Override
344 public short getHoldTime() {
345 return this.holdTime;
346 }
347
348 @Override
349 public boolean getLargeASCapability() {
350 return this.largeAs;
351 }
352
353 @Override
354 public void setLargeASCapability(boolean largeAs) {
355 this.largeAs = largeAs;
356 }
357
358 @Override
359 public boolean isPeerConfigured(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530360 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530361 return (lspeer != null) ? true : false;
362 }
363
364 @Override
365 public boolean isPeerConnected(String routerid) {
366 // TODO: is peer connected
367 return true;
368 }
369
370 @Override
371 public int getMaxConnRetryCount() {
372 return this.maxConnRetryCount;
373 }
374
375 @Override
376 public void setMaxConnRetryCout(int retryCount) {
377 this.maxConnRetryCount = retryCount;
378 }
379
380 @Override
381 public int getMaxConnRetryTime() {
382 return this.maxConnRetryTime;
383 }
384
385 @Override
386 public void setMaxConnRetryTime(int retryTime) {
387 this.maxConnRetryTime = retryTime;
388 }
389}