Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.bgp.controller.impl; |
| 17 | |
| 18 | import java.util.Iterator; |
| 19 | import java.util.Map.Entry; |
| 20 | import java.util.Set; |
| 21 | import java.util.TreeMap; |
| 22 | |
| 23 | import org.onlab.packet.Ip4Address; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 25 | import org.onosproject.bgp.controller.BgpCfg; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 26 | import org.onosproject.bgp.controller.BgpConnectPeer; |
| 27 | import org.onosproject.bgp.controller.BgpController; |
| 28 | import org.onosproject.bgp.controller.BgpId; |
| 29 | import org.onosproject.bgp.controller.BgpPeer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 30 | import org.onosproject.bgp.controller.BgpPeerCfg; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 31 | import org.onosproject.bgp.controller.impl.BgpControllerImpl.BgpPeerManagerImpl; |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | /** |
| 36 | * Provides BGP configuration of this BGP speaker. |
| 37 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 38 | public class BgpConfig implements BgpCfg { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 39 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 40 | protected static final Logger log = LoggerFactory.getLogger(BgpConfig.class); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 41 | |
| 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 56 | private TreeMap<String, BgpPeerCfg> bgpPeerTree = new TreeMap<>(); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 57 | private BgpConnectPeer connectPeer; |
| 58 | private BgpPeerManagerImpl peerManager; |
| 59 | private BgpController bgpController; |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 60 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 61 | /* |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 62 | * Constructor to initialize the values. |
| 63 | */ |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 64 | public BgpConfig(BgpController bgpController) { |
| 65 | this.bgpController = bgpController; |
| 66 | this.peerManager = (BgpPeerManagerImpl) bgpController.peerManager(); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 67 | this.holdTime = DEFAULT_HOLD_TIMER; |
| 68 | this.maxConnRetryTime = DEFAULT_CONN_RETRY_TIME; |
| 69 | this.maxConnRetryCount = DEFAULT_CONN_RETRY_COUNT; |
Shashikanth VH | cea18d3 | 2015-12-04 15:45:14 +0530 | [diff] [blame^] | 70 | this.lsCapability = true; |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 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 |
| 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 156 | BgpPeerConfig lspeer = new BgpPeerConfig(); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 157 | if (this.bgpPeerTree.get(routerid) == null) { |
| 158 | |
| 159 | lspeer.setPeerRouterId(routerid); |
| 160 | lspeer.setAsNumber(remoteAs); |
| 161 | lspeer.setHoldtime(holdTime); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 162 | lspeer.setState(BgpPeerCfg.State.IDLE); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 163 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 182 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 183 | |
| 184 | if (lspeer != null) { |
| 185 | lspeer.setSelfInnitConnection(true); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 186 | |
| 187 | if (lspeer.connectPeer() == null) { |
| 188 | connectPeer = new BgpConnectPeerImpl(bgpController, routerid, Controller.getBgpPortNum()); |
| 189 | lspeer.setConnectPeer(connectPeer); |
| 190 | connectPeer.connectPeer(); |
| 191 | } |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 192 | return true; |
| 193 | } |
| 194 | |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public boolean removePeer(String routerid) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 200 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 201 | |
| 202 | if (lspeer != null) { |
| 203 | |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 204 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 218 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 219 | |
| 220 | if (lspeer != null) { |
| 221 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 222 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 228 | lspeer.setState(BgpPeerCfg.State.IDLE); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 229 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 240 | public void setPeerConnState(String routerid, BgpPeerCfg.State state) { |
| 241 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 242 | |
| 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 255 | public BgpPeerCfg.State getPeerConnState(String routerid) { |
| 256 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 257 | |
| 258 | if (lspeer != null) { |
| 259 | return lspeer.getState(); |
| 260 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 261 | return BgpPeerCfg.State.INVALID; //No instance |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public boolean isPeerConnectable(String routerid) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 267 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 268 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 269 | if ((lspeer != null) && lspeer.getState().equals(BgpPeerCfg.State.IDLE)) { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 270 | return true; |
| 271 | } |
| 272 | |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 277 | public TreeMap<String, BgpPeerCfg> getPeerTree() { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 278 | return this.bgpPeerTree; |
| 279 | } |
| 280 | |
| 281 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 282 | public TreeMap<String, BgpPeerCfg> displayPeers() { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 283 | if (this.bgpPeerTree.isEmpty()) { |
| 284 | log.debug("There are no BGP peers"); |
| 285 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 286 | Set<Entry<String, BgpPeerCfg>> set = this.bgpPeerTree.entrySet(); |
| 287 | Iterator<Entry<String, BgpPeerCfg>> list = set.iterator(); |
| 288 | BgpPeerCfg lspeer; |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 289 | |
| 290 | while (list.hasNext()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 291 | Entry<String, BgpPeerCfg> me = list.next(); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 292 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 303 | public BgpPeerCfg displayPeers(String routerid) { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 304 | |
| 305 | if (this.bgpPeerTree.isEmpty()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 306 | log.debug("There are no Bgp peers"); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 307 | } 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 335 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 336 | 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 | } |