Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 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; |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame^] | 60 | private boolean rpdCapability; |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 61 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 62 | /* |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 63 | * Constructor to initialize the values. |
| 64 | */ |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 65 | public BgpConfig(BgpController bgpController) { |
| 66 | this.bgpController = bgpController; |
| 67 | this.peerManager = (BgpPeerManagerImpl) bgpController.peerManager(); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 68 | 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 123 | 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 VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame^] | 133 | 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 K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 143 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 176 | BgpPeerConfig lspeer = new BgpPeerConfig(); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 177 | if (this.bgpPeerTree.get(routerid) == null) { |
| 178 | |
| 179 | lspeer.setPeerRouterId(routerid); |
| 180 | lspeer.setAsNumber(remoteAs); |
| 181 | lspeer.setHoldtime(holdTime); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 182 | lspeer.setState(BgpPeerCfg.State.IDLE); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 183 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 202 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 203 | |
| 204 | if (lspeer != null) { |
| 205 | lspeer.setSelfInnitConnection(true); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 206 | |
| 207 | if (lspeer.connectPeer() == null) { |
| 208 | connectPeer = new BgpConnectPeerImpl(bgpController, routerid, Controller.getBgpPortNum()); |
| 209 | lspeer.setConnectPeer(connectPeer); |
| 210 | connectPeer.connectPeer(); |
| 211 | } |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 212 | return true; |
| 213 | } |
| 214 | |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public boolean removePeer(String routerid) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 220 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 221 | |
| 222 | if (lspeer != null) { |
| 223 | |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 224 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 238 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 239 | |
| 240 | if (lspeer != null) { |
| 241 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 242 | BgpPeer disconnPeer = peerManager.getPeer(BgpId.bgpId(IpAddress.valueOf(routerid))); |
| 243 | if (disconnPeer != null) { |
| 244 | // TODO: send notification peer deconfigured |
| 245 | disconnPeer.disconnectPeer(); |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 246 | } else if (lspeer.connectPeer() != null) { |
Shashikanth VH | 97e571e | 2016-01-05 12:15:14 +0530 | [diff] [blame] | 247 | lspeer.connectPeer().disconnectPeer(); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 248 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 249 | lspeer.setState(BgpPeerCfg.State.IDLE); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 250 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 261 | public void setPeerConnState(String routerid, BgpPeerCfg.State state) { |
| 262 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 263 | |
| 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 276 | public BgpPeerCfg.State getPeerConnState(String routerid) { |
| 277 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 278 | |
| 279 | if (lspeer != null) { |
| 280 | return lspeer.getState(); |
| 281 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 282 | return BgpPeerCfg.State.INVALID; //No instance |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public boolean isPeerConnectable(String routerid) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 288 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 289 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 290 | if ((lspeer != null) && lspeer.getState().equals(BgpPeerCfg.State.IDLE)) { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 291 | return true; |
| 292 | } |
| 293 | |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 298 | public TreeMap<String, BgpPeerCfg> getPeerTree() { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 299 | return this.bgpPeerTree; |
| 300 | } |
| 301 | |
| 302 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 303 | public TreeMap<String, BgpPeerCfg> displayPeers() { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 304 | if (this.bgpPeerTree.isEmpty()) { |
| 305 | log.debug("There are no BGP peers"); |
| 306 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 307 | Set<Entry<String, BgpPeerCfg>> set = this.bgpPeerTree.entrySet(); |
| 308 | Iterator<Entry<String, BgpPeerCfg>> list = set.iterator(); |
| 309 | BgpPeerCfg lspeer; |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 310 | |
| 311 | while (list.hasNext()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 312 | Entry<String, BgpPeerCfg> me = list.next(); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 313 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 324 | public BgpPeerCfg displayPeers(String routerid) { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 325 | |
| 326 | if (this.bgpPeerTree.isEmpty()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 327 | log.debug("There are no Bgp peers"); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 328 | } 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 356 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 357 | 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 | } |