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 | |
| 19 | import static org.onlab.util.Tools.groupedThreads; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame^] | 20 | |
| 21 | import java.util.concurrent.ConcurrentHashMap; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 22 | import java.util.concurrent.ExecutorService; |
| 23 | import java.util.concurrent.Executors; |
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; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Service; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame^] | 30 | import org.onlab.packet.IpAddress; |
Satish K | e107e66 | 2015-09-21 19:00:17 +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 | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame^] | 34 | import org.onosproject.bgp.controller.BGPPacketStats; |
| 35 | import org.onosproject.bgp.controller.BGPPeer; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 36 | import org.onosproject.bgpio.protocol.BGPMessage; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame^] | 37 | import org.onosproject.bgpio.protocol.BGPVersion; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
| 41 | @Component(immediate = true) |
| 42 | @Service |
| 43 | public class BGPControllerImpl implements BGPController { |
| 44 | |
| 45 | private static final Logger log = LoggerFactory.getLogger(BGPControllerImpl.class); |
| 46 | |
| 47 | private final ExecutorService executorMsgs = Executors.newFixedThreadPool(32, |
| 48 | groupedThreads("onos/bgp", |
| 49 | "event-stats-%d")); |
| 50 | |
| 51 | private final ExecutorService executorBarrier = Executors.newFixedThreadPool(4, |
| 52 | groupedThreads("onos/bgp", |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame^] | 53 | "event-barrier-%d")); |
| 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 | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame^] | 56 | protected BGPPeerManager peerManager = new BGPPeerManager(); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 57 | final Controller ctrl = new Controller(this); |
| 58 | |
| 59 | private BGPConfig bgpconfig = new BGPConfig(); |
| 60 | |
| 61 | @Activate |
| 62 | public void activate() { |
| 63 | this.ctrl.start(); |
| 64 | log.info("Started"); |
| 65 | } |
| 66 | |
| 67 | @Deactivate |
| 68 | public void deactivate() { |
| 69 | // Close all connected peers |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame^] | 70 | closeConnectedPeers(); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 71 | this.ctrl.stop(); |
| 72 | log.info("Stopped"); |
| 73 | } |
| 74 | |
| 75 | @Override |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame^] | 76 | public Iterable<BGPPeer> getPeers() { |
| 77 | return this.connectedPeers.values(); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public BGPPeer getPeer(BGPId bgpId) { |
| 82 | return this.connectedPeers.get(bgpId); |
| 83 | } |
| 84 | |
| 85 | @Override |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 86 | public void writeMsg(BGPId bgpId, BGPMessage msg) { |
| 87 | // TODO: Send message |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void processBGPPacket(BGPId bgpId, BGPMessage msg) { |
| 92 | |
| 93 | switch (msg.getType()) { |
| 94 | case OPEN: |
| 95 | // TODO: Process Open message |
| 96 | break; |
| 97 | case KEEP_ALIVE: |
| 98 | // TODO: Process keepalive message |
| 99 | break; |
| 100 | case NOTIFICATION: |
| 101 | // TODO: Process notificatoin message |
| 102 | break; |
| 103 | case UPDATE: |
| 104 | // TODO: Process update message |
| 105 | break; |
| 106 | default: |
| 107 | // TODO: Process other message |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame^] | 112 | @Override |
| 113 | public void closeConnectedPeers() { |
| 114 | BGPPeer bgpPeer; |
| 115 | for (BGPId id : this.connectedPeers.keySet()) { |
| 116 | bgpPeer = getPeer(id); |
| 117 | bgpPeer.disconnectPeer(); |
| 118 | } |
| 119 | } |
| 120 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 121 | /** |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame^] | 122 | * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which |
| 123 | * they are. |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 124 | */ |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame^] | 125 | public class BGPPeerManager { |
| 126 | |
| 127 | private final Logger log = LoggerFactory.getLogger(BGPPeerManager.class); |
| 128 | private final Lock peerLock = new ReentrantLock(); |
| 129 | |
| 130 | /** |
| 131 | * Add a BGP peer that has just connected to the system. |
| 132 | * |
| 133 | * @param bgpId the id of bgp peer to add |
| 134 | * @param bgpPeer the actual bgp peer object. |
| 135 | * @return true if added, false otherwise. |
| 136 | */ |
| 137 | public boolean addConnectedPeer(BGPId bgpId, BGPPeer bgpPeer) { |
| 138 | |
| 139 | if (connectedPeers.get(bgpId) != null) { |
| 140 | this.log.error("Trying to add connectedPeer but found previous " + "value for bgp ip: {}", |
| 141 | bgpId.toString()); |
| 142 | return false; |
| 143 | } else { |
| 144 | this.log.debug("Added Peer {}", bgpId.toString()); |
| 145 | connectedPeers.put(bgpId, bgpPeer); |
| 146 | return true; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Checks if the activation for this bgp peer is valid. |
| 152 | * |
| 153 | * @param bgpId the id of bgp peer to check |
| 154 | * @return true if valid, false otherwise |
| 155 | */ |
| 156 | public boolean isPeerConnected(BGPId bgpId) { |
| 157 | if (connectedPeers.get(bgpId) == null) { |
| 158 | this.log.error("Trying to activate peer but is not in " + "connected peer: bgpIp {}. Aborting ..", |
| 159 | bgpId.toString()); |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Checks if the activation for this bgp peer is valid. |
| 168 | * |
| 169 | * @param routerid the routerid of bgp peer to check |
| 170 | * @return true if valid, false otherwise |
| 171 | */ |
| 172 | public boolean isPeerConnected(String routerid) { |
| 173 | |
| 174 | final BGPId bgpId; |
| 175 | bgpId = BGPId.bgpId(IpAddress.valueOf(routerid)); |
| 176 | |
| 177 | if (connectedPeers.get(bgpId) != null) { |
| 178 | this.log.info("Peer connection exist "); |
| 179 | return true; |
| 180 | } |
| 181 | this.log.info("Initiate connect request to " + "peer: bgpIp {}", bgpId.toString()); |
| 182 | |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Clear all state in controller peer maps for a bgp peer that has |
| 188 | * disconnected from the local controller. |
| 189 | * |
| 190 | * @param bgpId the id of bgp peer to remove. |
| 191 | */ |
| 192 | public void removeConnectedPeer(BGPId bgpId) { |
| 193 | connectedPeers.remove(bgpId); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Clear all state in controller peer maps for a bgp peer that has |
| 198 | * disconnected from the local controller. |
| 199 | * |
| 200 | * @param routerid the router id of bgp peer to remove. |
| 201 | */ |
| 202 | public void removeConnectedPeer(String routerid) { |
| 203 | final BGPId bgpId; |
| 204 | |
| 205 | bgpId = BGPId.bgpId(IpAddress.valueOf(routerid)); |
| 206 | |
| 207 | connectedPeers.remove(bgpId); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Gets bgp peer for connected peer map. |
| 212 | * |
| 213 | * @param routerid router id |
| 214 | * @return peer if available, null otherwise |
| 215 | */ |
| 216 | public BGPPeer getPeer(String routerid) { |
| 217 | final BGPId bgpId; |
| 218 | bgpId = BGPId.bgpId(IpAddress.valueOf(routerid)); |
| 219 | |
| 220 | return connectedPeers.get(bgpId); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Gets bgp peer instance. |
| 225 | * |
| 226 | * @param bgpId bgp identifier. |
| 227 | * @param pv bgp version. |
| 228 | * @param pktStats packet statistics. |
| 229 | * @return BGPPeer peer instance. |
| 230 | */ |
| 231 | public BGPPeer getBGPPeerInstance(BGPId bgpId, BGPVersion pv, BGPPacketStats pktStats) { |
| 232 | BGPPeer bgpPeer = new BGPPeerImpl(); |
| 233 | bgpPeer.init(bgpId, pv, pktStats); |
| 234 | return bgpPeer; |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Gets controller instance. |
| 241 | * |
| 242 | * @return Controller instance. |
| 243 | */ |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 244 | public Controller getController() { |
| 245 | return ctrl; |
| 246 | } |
| 247 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame^] | 248 | /** |
| 249 | * Gets connected peers. |
| 250 | * |
| 251 | * @return connectedPeers from connected Peers Map. |
| 252 | */ |
| 253 | public ConcurrentHashMap<BGPId, BGPPeer> getConnectedPeers() { |
| 254 | return connectedPeers; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Gets peer manager. |
| 259 | * |
| 260 | * @return peerManager. |
| 261 | */ |
| 262 | public BGPPeerManager getPeerManager() { |
| 263 | return peerManager; |
| 264 | } |
| 265 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 266 | @Override |
| 267 | public BGPCfg getConfig() { |
| 268 | return this.bgpconfig; |
| 269 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame^] | 270 | |
| 271 | @Override |
| 272 | public int getBGPConnNumber() { |
| 273 | return connectedPeers.size(); |
| 274 | } |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 275 | } |