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 | f62b5c0 | 2015-11-20 22:23:08 +0530 | [diff] [blame] | 19 | import java.util.Set; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 20 | import java.util.concurrent.ConcurrentHashMap; |
Shashikanth VH | f62b5c0 | 2015-11-20 22:23:08 +0530 | [diff] [blame] | 21 | import java.util.concurrent.CopyOnWriteArraySet; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 22 | import java.util.concurrent.locks.Lock; |
| 23 | import java.util.concurrent.locks.ReentrantLock; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 24 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Service; |
| 29 | import org.onosproject.bgp.controller.BGPCfg; |
| 30 | import org.onosproject.bgp.controller.BGPController; |
| 31 | import org.onosproject.bgp.controller.BGPId; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 32 | import org.onosproject.bgp.controller.BGPPeer; |
Shashikanth VH | f62b5c0 | 2015-11-20 22:23:08 +0530 | [diff] [blame] | 33 | import org.onosproject.bgp.controller.BgpLinkListener; |
| 34 | import org.onosproject.bgp.controller.BgpNodeListener; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 35 | import org.onosproject.bgp.controller.BgpPeerManager; |
| 36 | import org.onosproject.bgpio.exceptions.BGPParseException; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 37 | import org.onosproject.bgpio.protocol.BGPMessage; |
| 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 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 47 | protected ConcurrentHashMap<BGPId, BGPPeer> connectedPeers = new ConcurrentHashMap<BGPId, BGPPeer>(); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 48 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 49 | protected BGPPeerManagerImpl peerManager = new BGPPeerManagerImpl(); |
Shashikanth VH | f62b5c0 | 2015-11-20 22:23:08 +0530 | [diff] [blame] | 50 | |
| 51 | protected Set<BgpNodeListener> bgpNodeListener = new CopyOnWriteArraySet<>(); |
| 52 | protected Set<BgpLinkListener> bgpLinkListener = new CopyOnWriteArraySet<>(); |
| 53 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 54 | final Controller ctrl = new Controller(this); |
| 55 | |
| 56 | private BGPConfig bgpconfig = new BGPConfig(); |
| 57 | |
| 58 | @Activate |
| 59 | public void activate() { |
| 60 | this.ctrl.start(); |
| 61 | log.info("Started"); |
| 62 | } |
| 63 | |
| 64 | @Deactivate |
| 65 | public void deactivate() { |
| 66 | // Close all connected peers |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 67 | closeConnectedPeers(); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 68 | this.ctrl.stop(); |
| 69 | log.info("Stopped"); |
| 70 | } |
| 71 | |
| 72 | @Override |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 73 | public Iterable<BGPPeer> getPeers() { |
| 74 | return this.connectedPeers.values(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public BGPPeer getPeer(BGPId bgpId) { |
| 79 | return this.connectedPeers.get(bgpId); |
| 80 | } |
| 81 | |
| 82 | @Override |
Shashikanth VH | f62b5c0 | 2015-11-20 22:23:08 +0530 | [diff] [blame] | 83 | public void addListener(BgpNodeListener listener) { |
| 84 | this.bgpNodeListener.add(listener); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void removeListener(BgpNodeListener listener) { |
| 89 | this.bgpNodeListener.remove(listener); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public Set<BgpNodeListener> listener() { |
| 94 | return bgpNodeListener; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void addLinkListener(BgpLinkListener listener) { |
| 99 | this.bgpLinkListener.add(listener); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void removeLinkListener(BgpLinkListener listener) { |
| 104 | this.bgpLinkListener.remove(listener); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public Set<BgpLinkListener> linkListener() { |
| 109 | return bgpLinkListener; |
| 110 | } |
| 111 | |
| 112 | @Override |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 113 | public void writeMsg(BGPId bgpId, BGPMessage msg) { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 114 | this.getPeer(bgpId).sendMessage(msg); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 118 | public void processBGPPacket(BGPId bgpId, BGPMessage msg) throws BGPParseException { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 119 | |
| 120 | switch (msg.getType()) { |
| 121 | case OPEN: |
| 122 | // TODO: Process Open message |
| 123 | break; |
| 124 | case KEEP_ALIVE: |
| 125 | // TODO: Process keepalive message |
| 126 | break; |
| 127 | case NOTIFICATION: |
| 128 | // TODO: Process notificatoin message |
| 129 | break; |
| 130 | case UPDATE: |
| 131 | // TODO: Process update message |
| 132 | break; |
| 133 | default: |
| 134 | // TODO: Process other message |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 139 | @Override |
| 140 | public void closeConnectedPeers() { |
| 141 | BGPPeer bgpPeer; |
| 142 | for (BGPId id : this.connectedPeers.keySet()) { |
| 143 | bgpPeer = getPeer(id); |
| 144 | bgpPeer.disconnectPeer(); |
| 145 | } |
| 146 | } |
| 147 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 148 | /** |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 149 | * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which |
| 150 | * they are. |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 151 | */ |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 152 | public class BGPPeerManagerImpl implements BgpPeerManager { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 153 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 154 | private final Logger log = LoggerFactory.getLogger(BGPPeerManagerImpl.class); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 155 | private final Lock peerLock = new ReentrantLock(); |
| 156 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 157 | @Override |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 158 | public boolean addConnectedPeer(BGPId bgpId, BGPPeer bgpPeer) { |
| 159 | |
| 160 | if (connectedPeers.get(bgpId) != null) { |
| 161 | this.log.error("Trying to add connectedPeer but found previous " + "value for bgp ip: {}", |
| 162 | bgpId.toString()); |
| 163 | return false; |
| 164 | } else { |
| 165 | this.log.debug("Added Peer {}", bgpId.toString()); |
| 166 | connectedPeers.put(bgpId, bgpPeer); |
| 167 | return true; |
| 168 | } |
| 169 | } |
| 170 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 171 | @Override |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 172 | public boolean isPeerConnected(BGPId bgpId) { |
| 173 | if (connectedPeers.get(bgpId) == null) { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 174 | this.log.error("Is peer connected: bgpIp {}.", bgpId.toString()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 175 | return false; |
| 176 | } |
| 177 | |
| 178 | return true; |
| 179 | } |
| 180 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 181 | @Override |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 182 | public void removeConnectedPeer(BGPId bgpId) { |
| 183 | connectedPeers.remove(bgpId); |
| 184 | } |
| 185 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 186 | @Override |
| 187 | public BGPPeer getPeer(BGPId bgpId) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 188 | return connectedPeers.get(bgpId); |
| 189 | } |
| 190 | |
| 191 | /** |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 192 | * Gets bgp peer instance. |
| 193 | * |
| 194 | * @param bgpController controller instance. |
| 195 | * @param sessionInfo bgp session info. |
| 196 | * @param pktStats packet statistics. |
| 197 | * @return BGPPeer peer instance. |
| 198 | */ |
| 199 | public BGPPeer getBGPPeerInstance(BGPController bgpController, BgpSessionInfoImpl sessionInfo, |
| 200 | BGPPacketStatsImpl pktStats) { |
| 201 | BGPPeer bgpPeer = new BGPPeerImpl(bgpController, sessionInfo, pktStats); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 202 | return bgpPeer; |
| 203 | } |
| 204 | |
| 205 | } |
| 206 | |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame^] | 207 | /** |
| 208 | * Returns controller. |
| 209 | * |
| 210 | * @return controller |
| 211 | */ |
| 212 | public Controller controller() { |
| 213 | return this.ctrl; |
| 214 | } |
| 215 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 216 | @Override |
| 217 | public ConcurrentHashMap<BGPId, BGPPeer> connectedPeers() { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 218 | return connectedPeers; |
| 219 | } |
| 220 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 221 | @Override |
| 222 | public BGPPeerManagerImpl peerManager() { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 223 | return peerManager; |
| 224 | } |
| 225 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 226 | @Override |
| 227 | public BGPCfg getConfig() { |
| 228 | return this.bgpconfig; |
| 229 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 230 | |
| 231 | @Override |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 232 | public int connectedPeerCount() { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 233 | return connectedPeers.size(); |
| 234 | } |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 235 | } |