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; |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 54 | private FlowSpec flowSpec = FlowSpec.NONE; |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 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; |
| 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 122 | 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 K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 132 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 165 | BgpPeerConfig lspeer = new BgpPeerConfig(); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 166 | if (this.bgpPeerTree.get(routerid) == null) { |
| 167 | |
| 168 | lspeer.setPeerRouterId(routerid); |
| 169 | lspeer.setAsNumber(remoteAs); |
| 170 | lspeer.setHoldtime(holdTime); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 171 | lspeer.setState(BgpPeerCfg.State.IDLE); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 172 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 191 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 192 | |
| 193 | if (lspeer != null) { |
| 194 | lspeer.setSelfInnitConnection(true); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 195 | |
| 196 | if (lspeer.connectPeer() == null) { |
| 197 | connectPeer = new BgpConnectPeerImpl(bgpController, routerid, Controller.getBgpPortNum()); |
| 198 | lspeer.setConnectPeer(connectPeer); |
| 199 | connectPeer.connectPeer(); |
| 200 | } |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 201 | return true; |
| 202 | } |
| 203 | |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public boolean removePeer(String routerid) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 209 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 210 | |
| 211 | if (lspeer != null) { |
| 212 | |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 213 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 227 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 228 | |
| 229 | if (lspeer != null) { |
| 230 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 231 | BgpPeer disconnPeer = peerManager.getPeer(BgpId.bgpId(IpAddress.valueOf(routerid))); |
| 232 | if (disconnPeer != null) { |
| 233 | // TODO: send notification peer deconfigured |
| 234 | disconnPeer.disconnectPeer(); |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 235 | } else if (lspeer.connectPeer() != null) { |
Shashikanth VH | 97e571e | 2016-01-05 12:15:14 +0530 | [diff] [blame] | 236 | lspeer.connectPeer().disconnectPeer(); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 237 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 238 | lspeer.setState(BgpPeerCfg.State.IDLE); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 239 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 250 | public void setPeerConnState(String routerid, BgpPeerCfg.State state) { |
| 251 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 252 | |
| 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 265 | public BgpPeerCfg.State getPeerConnState(String routerid) { |
| 266 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 267 | |
| 268 | if (lspeer != null) { |
| 269 | return lspeer.getState(); |
| 270 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 271 | return BgpPeerCfg.State.INVALID; //No instance |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public boolean isPeerConnectable(String routerid) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 277 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 278 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 279 | if ((lspeer != null) && lspeer.getState().equals(BgpPeerCfg.State.IDLE)) { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 280 | return true; |
| 281 | } |
| 282 | |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 287 | public TreeMap<String, BgpPeerCfg> getPeerTree() { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 288 | return this.bgpPeerTree; |
| 289 | } |
| 290 | |
| 291 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 292 | public TreeMap<String, BgpPeerCfg> displayPeers() { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 293 | if (this.bgpPeerTree.isEmpty()) { |
| 294 | log.debug("There are no BGP peers"); |
| 295 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 296 | Set<Entry<String, BgpPeerCfg>> set = this.bgpPeerTree.entrySet(); |
| 297 | Iterator<Entry<String, BgpPeerCfg>> list = set.iterator(); |
| 298 | BgpPeerCfg lspeer; |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 299 | |
| 300 | while (list.hasNext()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 301 | Entry<String, BgpPeerCfg> me = list.next(); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 302 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 313 | public BgpPeerCfg displayPeers(String routerid) { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 314 | |
| 315 | if (this.bgpPeerTree.isEmpty()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 316 | log.debug("There are no Bgp peers"); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 317 | } 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 345 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 346 | 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 | } |