Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Satish K | e107e66 | 2015-09-21 19:00:17 +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 | |
| 17 | package org.onosproject.bgp.controller.impl; |
| 18 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Service; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 23 | import org.onosproject.bgp.controller.BgpCfg; |
| 24 | import org.onosproject.bgp.controller.BgpController; |
| 25 | import org.onosproject.bgp.controller.BgpId; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 26 | import org.onosproject.bgp.controller.BgpLinkListener; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 27 | import org.onosproject.bgp.controller.BgpLocalRib; |
Shashikanth VH | f62b5c0 | 2015-11-20 22:23:08 +0530 | [diff] [blame] | 28 | import org.onosproject.bgp.controller.BgpNodeListener; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 29 | import org.onosproject.bgp.controller.BgpPeer; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 30 | import org.onosproject.bgp.controller.BgpPeerManager; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 31 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 32 | import org.onosproject.bgpio.protocol.BgpMessage; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 33 | import org.onosproject.bgpio.protocol.BgpUpdateMsg; |
| 34 | import org.onosproject.bgpio.types.BgpValueType; |
| 35 | import org.onosproject.bgpio.types.MpReachNlri; |
| 36 | import org.onosproject.bgpio.types.MpUnReachNlri; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 40 | import java.util.Iterator; |
| 41 | import java.util.List; |
| 42 | import java.util.Set; |
| 43 | import java.util.concurrent.ConcurrentHashMap; |
| 44 | import java.util.concurrent.CopyOnWriteArraySet; |
| 45 | import java.util.concurrent.locks.Lock; |
| 46 | import java.util.concurrent.locks.ReentrantLock; |
| 47 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 48 | @Component(immediate = true) |
| 49 | @Service |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 50 | public class BgpControllerImpl implements BgpController { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 51 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 52 | private static final Logger log = LoggerFactory.getLogger(BgpControllerImpl.class); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 53 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 54 | protected ConcurrentHashMap<BgpId, BgpPeer> connectedPeers = new ConcurrentHashMap<BgpId, BgpPeer>(); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 55 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 56 | protected BgpPeerManagerImpl peerManager = new BgpPeerManagerImpl(); |
Shashikanth VH | f62b5c0 | 2015-11-20 22:23:08 +0530 | [diff] [blame] | 57 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 58 | private BgpLocalRib bgplocalRib = new BgpLocalRibImpl(this); |
| 59 | private BgpLocalRib bgplocalRibVpn = new BgpLocalRibImpl(this); |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 60 | |
Shashikanth VH | f62b5c0 | 2015-11-20 22:23:08 +0530 | [diff] [blame] | 61 | protected Set<BgpNodeListener> bgpNodeListener = new CopyOnWriteArraySet<>(); |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 62 | protected Set<BgpLinkListener> bgpLinkListener = new CopyOnWriteArraySet<>(); |
Shashikanth VH | f62b5c0 | 2015-11-20 22:23:08 +0530 | [diff] [blame] | 63 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 64 | final Controller ctrl = new Controller(this); |
| 65 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 66 | private BgpConfig bgpconfig = new BgpConfig(this); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 67 | |
| 68 | @Activate |
| 69 | public void activate() { |
| 70 | this.ctrl.start(); |
| 71 | log.info("Started"); |
| 72 | } |
| 73 | |
| 74 | @Deactivate |
| 75 | public void deactivate() { |
| 76 | // Close all connected peers |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 77 | closeConnectedPeers(); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 78 | this.ctrl.stop(); |
| 79 | log.info("Stopped"); |
| 80 | } |
| 81 | |
| 82 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 83 | public Iterable<BgpPeer> getPeers() { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 84 | return this.connectedPeers.values(); |
| 85 | } |
| 86 | |
| 87 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 88 | public BgpPeer getPeer(BgpId bgpId) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 89 | return this.connectedPeers.get(bgpId); |
| 90 | } |
| 91 | |
| 92 | @Override |
Shashikanth VH | f62b5c0 | 2015-11-20 22:23:08 +0530 | [diff] [blame] | 93 | public void addListener(BgpNodeListener listener) { |
| 94 | this.bgpNodeListener.add(listener); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void removeListener(BgpNodeListener listener) { |
| 99 | this.bgpNodeListener.remove(listener); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public Set<BgpNodeListener> listener() { |
| 104 | return bgpNodeListener; |
| 105 | } |
| 106 | |
| 107 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 108 | public void writeMsg(BgpId bgpId, BgpMessage msg) { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 109 | this.getPeer(bgpId).sendMessage(msg); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 113 | public void processBgpPacket(BgpId bgpId, BgpMessage msg) throws BgpParseException { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 114 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 115 | BgpPeer peer = getPeer(bgpId); |
| 116 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 117 | switch (msg.getType()) { |
| 118 | case OPEN: |
| 119 | // TODO: Process Open message |
| 120 | break; |
| 121 | case KEEP_ALIVE: |
| 122 | // TODO: Process keepalive message |
| 123 | break; |
| 124 | case NOTIFICATION: |
| 125 | // TODO: Process notificatoin message |
| 126 | break; |
| 127 | case UPDATE: |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 128 | BgpUpdateMsg updateMsg = (BgpUpdateMsg) msg; |
| 129 | List<BgpValueType> pathAttr = updateMsg.bgpPathAttributes().pathAttributes(); |
| 130 | if (pathAttr == null) { |
| 131 | log.debug("llPathAttr is null, cannot process update message"); |
| 132 | break; |
| 133 | } |
| 134 | Iterator<BgpValueType> listIterator = pathAttr.iterator(); |
| 135 | boolean isLinkstate = false; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 136 | |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 137 | while (listIterator.hasNext()) { |
| 138 | BgpValueType attr = listIterator.next(); |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 139 | if (attr instanceof MpReachNlri) { |
| 140 | MpReachNlri mpReach = (MpReachNlri) attr; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 141 | if (mpReach.bgpFlowSpecNlri() == null) { |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 142 | isLinkstate = true; |
| 143 | } |
| 144 | } else if (attr instanceof MpUnReachNlri) { |
| 145 | MpUnReachNlri mpUnReach = (MpUnReachNlri) attr; |
Shashikanth VH | 26fd38a | 2016-04-26 18:11:37 +0530 | [diff] [blame] | 146 | if (mpUnReach.bgpFlowSpecNlri() == null) { |
Shashikanth VH | afb2e00 | 2016-02-12 14:48:29 +0530 | [diff] [blame] | 147 | isLinkstate = true; |
| 148 | } |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | if (isLinkstate) { |
| 152 | peer.buildAdjRibIn(pathAttr); |
| 153 | } |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 154 | break; |
| 155 | default: |
| 156 | // TODO: Process other message |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 161 | @Override |
| 162 | public void closeConnectedPeers() { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 163 | BgpPeer bgpPeer; |
| 164 | for (BgpId id : this.connectedPeers.keySet()) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 165 | bgpPeer = getPeer(id); |
| 166 | bgpPeer.disconnectPeer(); |
| 167 | } |
| 168 | } |
| 169 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 170 | /** |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 171 | * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which |
| 172 | * they are. |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 173 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 174 | public class BgpPeerManagerImpl implements BgpPeerManager { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 175 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 176 | private final Logger log = LoggerFactory.getLogger(BgpPeerManagerImpl.class); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 177 | private final Lock peerLock = new ReentrantLock(); |
| 178 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 179 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 180 | public boolean addConnectedPeer(BgpId bgpId, BgpPeer bgpPeer) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 181 | |
| 182 | if (connectedPeers.get(bgpId) != null) { |
| 183 | this.log.error("Trying to add connectedPeer but found previous " + "value for bgp ip: {}", |
| 184 | bgpId.toString()); |
| 185 | return false; |
| 186 | } else { |
| 187 | this.log.debug("Added Peer {}", bgpId.toString()); |
| 188 | connectedPeers.put(bgpId, bgpPeer); |
| 189 | return true; |
| 190 | } |
| 191 | } |
| 192 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 193 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 194 | public boolean isPeerConnected(BgpId bgpId) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 195 | if (connectedPeers.get(bgpId) == null) { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 196 | this.log.error("Is peer connected: bgpIp {}.", bgpId.toString()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | |
| 200 | return true; |
| 201 | } |
| 202 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 203 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 204 | public void removeConnectedPeer(BgpId bgpId) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 205 | connectedPeers.remove(bgpId); |
| 206 | } |
| 207 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 208 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 209 | public BgpPeer getPeer(BgpId bgpId) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 210 | return connectedPeers.get(bgpId); |
| 211 | } |
| 212 | |
| 213 | /** |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 214 | * Gets bgp peer instance. |
| 215 | * |
| 216 | * @param bgpController controller instance. |
| 217 | * @param sessionInfo bgp session info. |
| 218 | * @param pktStats packet statistics. |
| 219 | * @return BGPPeer peer instance. |
| 220 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 221 | public BgpPeer getBgpPeerInstance(BgpController bgpController, BgpSessionInfoImpl sessionInfo, |
| 222 | BgpPacketStatsImpl pktStats) { |
| 223 | BgpPeer bgpPeer = new BgpPeerImpl(bgpController, sessionInfo, pktStats); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 224 | return bgpPeer; |
| 225 | } |
| 226 | |
| 227 | } |
| 228 | |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 229 | /** |
| 230 | * Returns controller. |
| 231 | * |
| 232 | * @return controller |
| 233 | */ |
| 234 | public Controller controller() { |
| 235 | return this.ctrl; |
| 236 | } |
| 237 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 238 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 239 | public ConcurrentHashMap<BgpId, BgpPeer> connectedPeers() { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 240 | return connectedPeers; |
| 241 | } |
| 242 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 243 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 244 | public BgpPeerManagerImpl peerManager() { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 245 | return peerManager; |
| 246 | } |
| 247 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 248 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 249 | public BgpCfg getConfig() { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 250 | return this.bgpconfig; |
| 251 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 252 | |
| 253 | @Override |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 254 | public int connectedPeerCount() { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 255 | return connectedPeers.size(); |
| 256 | } |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 257 | |
| 258 | /** |
| 259 | * Gets the BGP local RIB. |
| 260 | * |
| 261 | * @return bgplocalRIB BGP local RIB. |
| 262 | */ |
| 263 | @Override |
| 264 | public BgpLocalRib bgpLocalRib() { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 265 | return bgplocalRib; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Gets the BGP local RIB with VPN. |
| 270 | * |
| 271 | * @return bgplocalRIBVpn BGP VPN local RIB . |
| 272 | */ |
| 273 | @Override |
| 274 | public BgpLocalRib bgpLocalRibVpn() { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 275 | return bgplocalRibVpn; |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 276 | } |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 277 | |
| 278 | @Override |
| 279 | public void addLinkListener(BgpLinkListener listener) { |
| 280 | this.bgpLinkListener.add(listener); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void removeLinkListener(BgpLinkListener listener) { |
| 285 | this.bgpLinkListener.remove(listener); |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public Set<BgpLinkListener> linkListener() { |
| 290 | return bgpLinkListener; |
| 291 | } |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 292 | } |