blob: 716cc0c575866e6a47a5182b292ec21a41771db7 [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 VH5dd8dbe2015-11-26 13:22:18 +053024import org.onosproject.bgp.controller.BgpCfg;
25import org.onosproject.bgp.controller.BgpPeerCfg;
Thejaswi N K6a4cd002015-09-21 17:19:55 +053026import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29/**
30 * Provides BGP configuration of this BGP speaker.
31 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053032public class BgpConfig implements BgpCfg {
Thejaswi N K6a4cd002015-09-21 17:19:55 +053033
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053034 protected static final Logger log = LoggerFactory.getLogger(BgpConfig.class);
Thejaswi N K6a4cd002015-09-21 17:19:55 +053035
36 private static final short DEFAULT_HOLD_TIMER = 120;
37 private static final short DEFAULT_CONN_RETRY_TIME = 120;
38 private static final short DEFAULT_CONN_RETRY_COUNT = 5;
39
40 private State state = State.INIT;
41 private int localAs;
42 private int maxSession;
43 private boolean lsCapability;
44 private short holdTime;
45 private boolean largeAs = false;
46 private int maxConnRetryTime;
47 private int maxConnRetryCount;
48
49 private Ip4Address routerId = null;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053050 private TreeMap<String, BgpPeerCfg> bgpPeerTree = new TreeMap<>();
Thejaswi N K6a4cd002015-09-21 17:19:55 +053051
52 /**
53 * Constructor to initialize the values.
54 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053055 public BgpConfig() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +053056
57 this.holdTime = DEFAULT_HOLD_TIMER;
58 this.maxConnRetryTime = DEFAULT_CONN_RETRY_TIME;
59 this.maxConnRetryCount = DEFAULT_CONN_RETRY_COUNT;
60 }
61
62 @Override
63 public State getState() {
64 return state;
65 }
66
67 @Override
68 public void setState(State state) {
69 this.state = state;
70 }
71
72 @Override
73 public int getAsNumber() {
74 return this.localAs;
75 }
76
77 @Override
78 public void setAsNumber(int localAs) {
79
80 State localState = getState();
81 this.localAs = localAs;
82
83 /* Set configuration state */
84 if (localState == State.IP_CONFIGURED) {
85 setState(State.IP_AS_CONFIGURED);
86 } else {
87 setState(State.AS_CONFIGURED);
88 }
89 }
90
91 @Override
92 public int getMaxSession() {
93 return this.maxSession;
94 }
95
96 @Override
97 public void setMaxSession(int maxSession) {
98 this.maxSession = maxSession;
99 }
100
101 @Override
102 public boolean getLsCapability() {
103 return this.lsCapability;
104 }
105
106 @Override
107 public void setLsCapability(boolean lsCapability) {
108 this.lsCapability = lsCapability;
109 }
110
111 @Override
112 public String getRouterId() {
113 if (this.routerId != null) {
114 return this.routerId.toString();
115 } else {
116 return null;
117 }
118 }
119
120 @Override
121 public void setRouterId(String routerId) {
122 State localState = getState();
123 this.routerId = Ip4Address.valueOf(routerId);
124
125 /* Set configuration state */
126 if (localState == State.AS_CONFIGURED) {
127 setState(State.IP_AS_CONFIGURED);
128 } else {
129 setState(State.IP_CONFIGURED);
130 }
131 }
132
133 @Override
134 public boolean addPeer(String routerid, int remoteAs) {
135 return addPeer(routerid, remoteAs, DEFAULT_HOLD_TIMER);
136 }
137
138 @Override
139 public boolean addPeer(String routerid, short holdTime) {
140 return addPeer(routerid, this.getAsNumber(), holdTime);
141 }
142
143 @Override
144 public boolean addPeer(String routerid, int remoteAs, short holdTime) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530145 BgpPeerConfig lspeer = new BgpPeerConfig();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530146 if (this.bgpPeerTree.get(routerid) == null) {
147
148 lspeer.setPeerRouterId(routerid);
149 lspeer.setAsNumber(remoteAs);
150 lspeer.setHoldtime(holdTime);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530151 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530152 lspeer.setSelfInnitConnection(false);
153
154 if (this.getAsNumber() == remoteAs) {
155 lspeer.setIsIBgp(true);
156 } else {
157 lspeer.setIsIBgp(false);
158 }
159
160 this.bgpPeerTree.put(routerid, lspeer);
161 log.debug("added successfully");
162 return true;
163 } else {
164 log.debug("already exists");
165 return false;
166 }
167 }
168
169 @Override
170 public boolean connectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530171 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530172
173 if (lspeer != null) {
174 lspeer.setSelfInnitConnection(true);
175 // TODO: initiate peer connection
176 return true;
177 }
178
179 return false;
180 }
181
182 @Override
183 public boolean removePeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530184 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530185
186 if (lspeer != null) {
187
188 //TODO DISCONNECT PEER
189 disconnectPeer(routerid);
190 lspeer.setSelfInnitConnection(false);
191 lspeer = this.bgpPeerTree.remove(routerid);
192 log.debug("Deleted : " + routerid + " successfully");
193
194 return true;
195 } else {
196 log.debug("Did not find : " + routerid);
197 return false;
198 }
199 }
200
201 @Override
202 public boolean disconnectPeer(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530203 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530204
205 if (lspeer != null) {
206
207 //TODO DISCONNECT PEER
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530208 lspeer.setState(BgpPeerCfg.State.IDLE);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530209 lspeer.setSelfInnitConnection(false);
210 log.debug("Disconnected : " + routerid + " successfully");
211
212 return true;
213 } else {
214 log.debug("Did not find : " + routerid);
215 return false;
216 }
217 }
218
219 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530220 public void setPeerConnState(String routerid, BgpPeerCfg.State state) {
221 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530222
223 if (lspeer != null) {
224 lspeer.setState(state);
225 log.debug("Peer : " + routerid + " is not available");
226
227 return;
228 } else {
229 log.debug("Did not find : " + routerid);
230 return;
231 }
232 }
233
234 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530235 public BgpPeerCfg.State getPeerConnState(String routerid) {
236 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530237
238 if (lspeer != null) {
239 return lspeer.getState();
240 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530241 return BgpPeerCfg.State.INVALID; //No instance
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530242 }
243 }
244
245 @Override
246 public boolean isPeerConnectable(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530247 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530248
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530249 if ((lspeer != null) && lspeer.getState().equals(BgpPeerCfg.State.IDLE)) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530250 return true;
251 }
252
253 return false;
254 }
255
256 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530257 public TreeMap<String, BgpPeerCfg> getPeerTree() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530258 return this.bgpPeerTree;
259 }
260
261 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530262 public TreeMap<String, BgpPeerCfg> displayPeers() {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530263 if (this.bgpPeerTree.isEmpty()) {
264 log.debug("There are no BGP peers");
265 } else {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530266 Set<Entry<String, BgpPeerCfg>> set = this.bgpPeerTree.entrySet();
267 Iterator<Entry<String, BgpPeerCfg>> list = set.iterator();
268 BgpPeerCfg lspeer;
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530269
270 while (list.hasNext()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530271 Entry<String, BgpPeerCfg> me = list.next();
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530272 lspeer = me.getValue();
273 log.debug("Peer neighbor IP :" + me.getKey());
274 log.debug(", AS Number : " + lspeer.getAsNumber());
275 log.debug(", Hold Timer : " + lspeer.getHoldtime());
276 log.debug(", Is iBGP : " + lspeer.getIsIBgp());
277 }
278 }
279 return null;
280 }
281
282 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530283 public BgpPeerCfg displayPeers(String routerid) {
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530284
285 if (this.bgpPeerTree.isEmpty()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530286 log.debug("There are no Bgp peers");
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530287 } else {
288 return this.bgpPeerTree.get(routerid);
289 }
290 return null;
291 }
292
293 @Override
294 public void setHoldTime(short holdTime) {
295 this.holdTime = holdTime;
296 }
297
298 @Override
299 public short getHoldTime() {
300 return this.holdTime;
301 }
302
303 @Override
304 public boolean getLargeASCapability() {
305 return this.largeAs;
306 }
307
308 @Override
309 public void setLargeASCapability(boolean largeAs) {
310 this.largeAs = largeAs;
311 }
312
313 @Override
314 public boolean isPeerConfigured(String routerid) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530315 BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid);
Thejaswi N K6a4cd002015-09-21 17:19:55 +0530316 return (lspeer != null) ? true : false;
317 }
318
319 @Override
320 public boolean isPeerConnected(String routerid) {
321 // TODO: is peer connected
322 return true;
323 }
324
325 @Override
326 public int getMaxConnRetryCount() {
327 return this.maxConnRetryCount;
328 }
329
330 @Override
331 public void setMaxConnRetryCout(int retryCount) {
332 this.maxConnRetryCount = retryCount;
333 }
334
335 @Override
336 public int getMaxConnRetryTime() {
337 return this.maxConnRetryTime;
338 }
339
340 @Override
341 public void setMaxConnRetryTime(int retryTime) {
342 this.maxConnRetryTime = retryTime;
343 }
344}