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