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