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; |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame^] | 22 | import java.util.List; |
| 23 | import java.util.ArrayList; |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 24 | |
| 25 | import org.onlab.packet.Ip4Address; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 27 | import org.onosproject.bgp.controller.BgpCfg; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 28 | import org.onosproject.bgp.controller.BgpConnectPeer; |
| 29 | import org.onosproject.bgp.controller.BgpController; |
| 30 | import org.onosproject.bgp.controller.BgpId; |
| 31 | import org.onosproject.bgp.controller.BgpPeer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 32 | import org.onosproject.bgp.controller.BgpPeerCfg; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 33 | import org.onosproject.bgp.controller.impl.BgpControllerImpl.BgpPeerManagerImpl; |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
| 37 | /** |
| 38 | * Provides BGP configuration of this BGP speaker. |
| 39 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 40 | public class BgpConfig implements BgpCfg { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 41 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 42 | protected static final Logger log = LoggerFactory.getLogger(BgpConfig.class); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 43 | |
| 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; |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame^] | 47 | private List<BgpConnectPeerImpl> peerList = new ArrayList(); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 48 | 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 56 | private FlowSpec flowSpec = FlowSpec.NONE; |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 57 | private Ip4Address routerId = null; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 58 | private TreeMap<String, BgpPeerCfg> bgpPeerTree = new TreeMap<>(); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 59 | private BgpConnectPeer connectPeer; |
| 60 | private BgpPeerManagerImpl peerManager; |
| 61 | private BgpController bgpController; |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 62 | private boolean rpdCapability; |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 63 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 64 | /* |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 65 | * Constructor to initialize the values. |
| 66 | */ |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 67 | public BgpConfig(BgpController bgpController) { |
| 68 | this.bgpController = bgpController; |
| 69 | this.peerManager = (BgpPeerManagerImpl) bgpController.peerManager(); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 70 | 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 VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 125 | 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 VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 135 | 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 K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 145 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 178 | BgpPeerConfig lspeer = new BgpPeerConfig(); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 179 | if (this.bgpPeerTree.get(routerid) == null) { |
| 180 | |
| 181 | lspeer.setPeerRouterId(routerid); |
| 182 | lspeer.setAsNumber(remoteAs); |
| 183 | lspeer.setHoldtime(holdTime); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 184 | lspeer.setState(BgpPeerCfg.State.IDLE); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 185 | 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); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame^] | 194 | log.debug("Added successfully"); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 195 | return true; |
| 196 | } else { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame^] | 197 | log.debug("Already exists"); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 198 | return false; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public boolean connectPeer(String routerid) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 204 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 205 | |
| 206 | if (lspeer != null) { |
| 207 | lspeer.setSelfInnitConnection(true); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 208 | |
| 209 | if (lspeer.connectPeer() == null) { |
Shashikanth VH | 83f77e4 | 2016-05-26 20:34:56 +0530 | [diff] [blame] | 210 | connectPeer = new BgpConnectPeerImpl(bgpController, routerid, Controller.BGP_PORT_NUM); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 211 | lspeer.setConnectPeer(connectPeer); |
| 212 | connectPeer.connectPeer(); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame^] | 213 | peerList.add((BgpConnectPeerImpl) connectPeer); |
| 214 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 215 | } |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 216 | return true; |
| 217 | } |
| 218 | |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public boolean removePeer(String routerid) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 224 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 225 | |
| 226 | if (lspeer != null) { |
| 227 | |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 228 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 242 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 243 | |
| 244 | if (lspeer != null) { |
| 245 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 246 | BgpPeer disconnPeer = peerManager.getPeer(BgpId.bgpId(IpAddress.valueOf(routerid))); |
| 247 | if (disconnPeer != null) { |
| 248 | // TODO: send notification peer deconfigured |
| 249 | disconnPeer.disconnectPeer(); |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 250 | } else if (lspeer.connectPeer() != null) { |
Shashikanth VH | 97e571e | 2016-01-05 12:15:14 +0530 | [diff] [blame] | 251 | lspeer.connectPeer().disconnectPeer(); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 252 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 253 | lspeer.setState(BgpPeerCfg.State.IDLE); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 254 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 265 | public void setPeerConnState(String routerid, BgpPeerCfg.State state) { |
| 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 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 280 | public BgpPeerCfg.State getPeerConnState(String routerid) { |
| 281 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 282 | |
| 283 | if (lspeer != null) { |
| 284 | return lspeer.getState(); |
| 285 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 286 | return BgpPeerCfg.State.INVALID; //No instance |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public boolean isPeerConnectable(String routerid) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 292 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 293 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 294 | if ((lspeer != null) && lspeer.getState().equals(BgpPeerCfg.State.IDLE)) { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 295 | return true; |
| 296 | } |
| 297 | |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 302 | public TreeMap<String, BgpPeerCfg> getPeerTree() { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 303 | return this.bgpPeerTree; |
| 304 | } |
| 305 | |
| 306 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 307 | public TreeMap<String, BgpPeerCfg> displayPeers() { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 308 | if (this.bgpPeerTree.isEmpty()) { |
| 309 | log.debug("There are no BGP peers"); |
| 310 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 311 | Set<Entry<String, BgpPeerCfg>> set = this.bgpPeerTree.entrySet(); |
| 312 | Iterator<Entry<String, BgpPeerCfg>> list = set.iterator(); |
| 313 | BgpPeerCfg lspeer; |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 314 | |
| 315 | while (list.hasNext()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 316 | Entry<String, BgpPeerCfg> me = list.next(); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 317 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 328 | public BgpPeerCfg displayPeers(String routerid) { |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 329 | |
| 330 | if (this.bgpPeerTree.isEmpty()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 331 | log.debug("There are no Bgp peers"); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 332 | } 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 360 | BgpPeerCfg lspeer = this.bgpPeerTree.get(routerid); |
Thejaswi N K | 6a4cd00 | 2015-09-21 17:19:55 +0530 | [diff] [blame] | 361 | 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 | } |