Committed by
Gerrit Code Review
[Emu] [ONOS-2591,ONOS-2594] Implementation of BGP channel handler to manage each…
… BGP peer connection. Change-Id: I14e90c9437f676698f89da79e736a81035689492
Showing
9 changed files
with
780 additions
and
33 deletions
| ... | @@ -25,6 +25,21 @@ import org.onosproject.bgpio.protocol.BGPMessage; | ... | @@ -25,6 +25,21 @@ import org.onosproject.bgpio.protocol.BGPMessage; |
| 25 | public interface BGPController { | 25 | public interface BGPController { |
| 26 | 26 | ||
| 27 | /** | 27 | /** |
| 28 | + * Returns list of bgp peers connected to this BGP controller. | ||
| 29 | + * | ||
| 30 | + * @return Iterable of BGPPeer elements | ||
| 31 | + */ | ||
| 32 | + Iterable<BGPPeer> getPeers(); | ||
| 33 | + | ||
| 34 | + /** | ||
| 35 | + * Returns the actual bgp peer for the given ip address. | ||
| 36 | + * | ||
| 37 | + * @param bgpId the id of the bgp peer to fetch | ||
| 38 | + * @return the interface to this bgp peer | ||
| 39 | + */ | ||
| 40 | + BGPPeer getPeer(BGPId bgpId); | ||
| 41 | + | ||
| 42 | + /** | ||
| 28 | * Send a message to a particular bgp peer. | 43 | * Send a message to a particular bgp peer. |
| 29 | * | 44 | * |
| 30 | * @param bgpId the id of the peer to send message. | 45 | * @param bgpId the id of the peer to send message. |
| ... | @@ -41,9 +56,22 @@ public interface BGPController { | ... | @@ -41,9 +56,22 @@ public interface BGPController { |
| 41 | void processBGPPacket(BGPId bgpId, BGPMessage msg); | 56 | void processBGPPacket(BGPId bgpId, BGPMessage msg); |
| 42 | 57 | ||
| 43 | /** | 58 | /** |
| 59 | + * Close all connected BGP peers. | ||
| 60 | + * | ||
| 61 | + */ | ||
| 62 | + void closeConnectedPeers(); | ||
| 63 | + | ||
| 64 | + /** | ||
| 44 | * Get the BGPConfig class to the caller. | 65 | * Get the BGPConfig class to the caller. |
| 45 | * | 66 | * |
| 46 | * @return configuration object | 67 | * @return configuration object |
| 47 | */ | 68 | */ |
| 48 | BGPCfg getConfig(); | 69 | BGPCfg getConfig(); |
| 70 | + | ||
| 71 | + /** | ||
| 72 | + * Get the BGP connected peers to this controller. | ||
| 73 | + * | ||
| 74 | + * @return the integer number | ||
| 75 | + */ | ||
| 76 | + int getBGPConnNumber(); | ||
| 49 | } | 77 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| 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 | +package org.onosproject.bgp.controller; | ||
| 17 | +import java.util.List; | ||
| 18 | +import org.jboss.netty.channel.Channel; | ||
| 19 | +import org.onosproject.bgpio.protocol.BGPMessage; | ||
| 20 | +import org.onosproject.bgpio.protocol.BGPVersion; | ||
| 21 | + | ||
| 22 | +/** | ||
| 23 | + * Represents the peer side of an bgp peer. | ||
| 24 | + * | ||
| 25 | + */ | ||
| 26 | +public interface BGPPeer { | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * Sets the BGP version for this bgp peer. | ||
| 30 | + * | ||
| 31 | + * @param bgpVersion the version to set. | ||
| 32 | + */ | ||
| 33 | + void setBgpPeerVersion(BGPVersion bgpVersion); | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * Gets the BGP version for this bgp peer. | ||
| 37 | + * | ||
| 38 | + * @return bgp identifier. | ||
| 39 | + */ | ||
| 40 | + int getBgpPeerIdentifier(); | ||
| 41 | + | ||
| 42 | + /** | ||
| 43 | + * Sets the associated Netty channel for this bgp peer. | ||
| 44 | + * | ||
| 45 | + * @param channel the Netty channel | ||
| 46 | + */ | ||
| 47 | + void setChannel(Channel channel); | ||
| 48 | + | ||
| 49 | + /** | ||
| 50 | + * Gets the associated Netty channel handler for this bgp peer. | ||
| 51 | + * | ||
| 52 | + * @return Channel channel connected. | ||
| 53 | + */ | ||
| 54 | + Channel getChannel(); | ||
| 55 | + | ||
| 56 | + /** | ||
| 57 | + * Sets the AS Number for this bgp peer. | ||
| 58 | + * | ||
| 59 | + * @param peerASNum the autonomous system number value to set. | ||
| 60 | + */ | ||
| 61 | + void setBgpPeerASNum(short peerASNum); | ||
| 62 | + | ||
| 63 | + /** | ||
| 64 | + * Sets the hold time for this bgp peer. | ||
| 65 | + * | ||
| 66 | + * @param peerHoldTime the hold timer value to set. | ||
| 67 | + */ | ||
| 68 | + void setBgpPeerHoldTime(short peerHoldTime); | ||
| 69 | + | ||
| 70 | + /** | ||
| 71 | + * Sets the peer identifier value. | ||
| 72 | + * | ||
| 73 | + * @param peerIdentifier the bgp peer identifier value. | ||
| 74 | + */ | ||
| 75 | + void setBgpPeerIdentifier(int peerIdentifier); | ||
| 76 | + | ||
| 77 | + /** | ||
| 78 | + * Sets whether the bgp peer is connected. | ||
| 79 | + * | ||
| 80 | + * @param connected whether the bgp peer is connected | ||
| 81 | + */ | ||
| 82 | + void setConnected(boolean connected); | ||
| 83 | + | ||
| 84 | + /** | ||
| 85 | + * Initialises the behaviour. | ||
| 86 | + * | ||
| 87 | + * @param bgpId id of bgp peer | ||
| 88 | + * @param bgpVersion BGP version | ||
| 89 | + * @param pktStats packet statistics | ||
| 90 | + */ | ||
| 91 | + void init(BGPId bgpId, BGPVersion bgpVersion, BGPPacketStats pktStats); | ||
| 92 | + | ||
| 93 | + /** | ||
| 94 | + * Checks whether the handshake is complete. | ||
| 95 | + * | ||
| 96 | + * @return true is finished, false if not. | ||
| 97 | + */ | ||
| 98 | + boolean isHandshakeComplete(); | ||
| 99 | + | ||
| 100 | + /** | ||
| 101 | + * Writes the message to the peer. | ||
| 102 | + * | ||
| 103 | + * @param msg the message to write | ||
| 104 | + */ | ||
| 105 | + void sendMessage(BGPMessage msg); | ||
| 106 | + | ||
| 107 | + /** | ||
| 108 | + * Writes the BGPMessage list to the peer. | ||
| 109 | + * | ||
| 110 | + * @param msgs the messages to be written | ||
| 111 | + */ | ||
| 112 | + void sendMessage(List<BGPMessage> msgs); | ||
| 113 | + | ||
| 114 | + /** | ||
| 115 | + * Gets a string version of the ID for this bgp peer. | ||
| 116 | + * | ||
| 117 | + * @return string version of the ID | ||
| 118 | + */ | ||
| 119 | + String getStringId(); | ||
| 120 | + | ||
| 121 | + /** | ||
| 122 | + * Gets the ipAddress of the peer. | ||
| 123 | + * | ||
| 124 | + * @return the peer bgpId in IPAddress format | ||
| 125 | + */ | ||
| 126 | + BGPId getBGPId(); | ||
| 127 | + | ||
| 128 | + /** | ||
| 129 | + * Checks if the bgp peer is still connected. | ||
| 130 | + * | ||
| 131 | + * @return whether the bgp peer is still connected | ||
| 132 | + */ | ||
| 133 | + boolean isConnected(); | ||
| 134 | + | ||
| 135 | + /** | ||
| 136 | + * Disconnects the bgp peer by closing the TCP connection. Results in a call to the channel handler's | ||
| 137 | + * channelDisconnected method for cleanup | ||
| 138 | + */ | ||
| 139 | + void disconnectPeer(); | ||
| 140 | + | ||
| 141 | + /** | ||
| 142 | + * Identifies the channel used to communicate with the bgp peer. | ||
| 143 | + * | ||
| 144 | + * @return string representation of the connection to the peer | ||
| 145 | + */ | ||
| 146 | + String channelId(); | ||
| 147 | + | ||
| 148 | + /** | ||
| 149 | + * Gets the negotiated hold time. | ||
| 150 | + * | ||
| 151 | + * @return the negotiated hold time | ||
| 152 | + */ | ||
| 153 | + int getNegotiatedHoldTime(); | ||
| 154 | + | ||
| 155 | + /** | ||
| 156 | + * Sets negotiated hold time for the peer. | ||
| 157 | + * | ||
| 158 | + * @param negotiatedHoldTime negotiated hold time | ||
| 159 | + */ | ||
| 160 | + void setNegotiatedHoldTime(short negotiatedHoldTime); | ||
| 161 | +} |
| ... | @@ -61,27 +61,6 @@ public interface BGPMessage extends Writeable { | ... | @@ -61,27 +61,6 @@ public interface BGPMessage extends Writeable { |
| 61 | BGPMessage build() throws BGPParseException; | 61 | BGPMessage build() throws BGPParseException; |
| 62 | 62 | ||
| 63 | /** | 63 | /** |
| 64 | - * Returns BGP Version of BGP Message. | ||
| 65 | - * | ||
| 66 | - * @return BGP Version of BGP Message | ||
| 67 | - */ | ||
| 68 | - BGPVersion getVersion(); | ||
| 69 | - | ||
| 70 | - /** | ||
| 71 | - * Returns BGP Type of BGP Message. | ||
| 72 | - * | ||
| 73 | - * @return BGP Type of BGP Message | ||
| 74 | - */ | ||
| 75 | - BGPType getType(); | ||
| 76 | - | ||
| 77 | - /** | ||
| 78 | - * Returns BGP Header of BGP Message. | ||
| 79 | - * | ||
| 80 | - * @return BGP Header of BGP Message | ||
| 81 | - */ | ||
| 82 | - BGPHeader getHeader(); | ||
| 83 | - | ||
| 84 | - /** | ||
| 85 | * Sets BgpHeader and return its builder. | 64 | * Sets BgpHeader and return its builder. |
| 86 | * | 65 | * |
| 87 | * @param bgpMsgHeader BGP Message Header | 66 | * @param bgpMsgHeader BGP Message Header | ... | ... |
This diff is collapsed. Click to expand it.
| ... | @@ -17,16 +17,24 @@ | ... | @@ -17,16 +17,24 @@ |
| 17 | package org.onosproject.bgp.controller.impl; | 17 | package org.onosproject.bgp.controller.impl; |
| 18 | 18 | ||
| 19 | import static org.onlab.util.Tools.groupedThreads; | 19 | import static org.onlab.util.Tools.groupedThreads; |
| 20 | + | ||
| 21 | +import java.util.concurrent.ConcurrentHashMap; | ||
| 20 | import java.util.concurrent.ExecutorService; | 22 | import java.util.concurrent.ExecutorService; |
| 21 | import java.util.concurrent.Executors; | 23 | import java.util.concurrent.Executors; |
| 24 | +import java.util.concurrent.locks.Lock; | ||
| 25 | +import java.util.concurrent.locks.ReentrantLock; | ||
| 22 | import org.apache.felix.scr.annotations.Activate; | 26 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; | 27 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; | 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Service; | 29 | import org.apache.felix.scr.annotations.Service; |
| 30 | +import org.onlab.packet.IpAddress; | ||
| 26 | import org.onosproject.bgp.controller.BGPCfg; | 31 | import org.onosproject.bgp.controller.BGPCfg; |
| 27 | import org.onosproject.bgp.controller.BGPController; | 32 | import org.onosproject.bgp.controller.BGPController; |
| 28 | import org.onosproject.bgp.controller.BGPId; | 33 | import org.onosproject.bgp.controller.BGPId; |
| 34 | +import org.onosproject.bgp.controller.BGPPacketStats; | ||
| 35 | +import org.onosproject.bgp.controller.BGPPeer; | ||
| 29 | import org.onosproject.bgpio.protocol.BGPMessage; | 36 | import org.onosproject.bgpio.protocol.BGPMessage; |
| 37 | +import org.onosproject.bgpio.protocol.BGPVersion; | ||
| 30 | import org.slf4j.Logger; | 38 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; | 39 | import org.slf4j.LoggerFactory; |
| 32 | 40 | ||
| ... | @@ -42,8 +50,10 @@ public class BGPControllerImpl implements BGPController { | ... | @@ -42,8 +50,10 @@ public class BGPControllerImpl implements BGPController { |
| 42 | 50 | ||
| 43 | private final ExecutorService executorBarrier = Executors.newFixedThreadPool(4, | 51 | private final ExecutorService executorBarrier = Executors.newFixedThreadPool(4, |
| 44 | groupedThreads("onos/bgp", | 52 | groupedThreads("onos/bgp", |
| 45 | - "event-barrier-%d")); | 53 | + "event-barrier-%d")); |
| 54 | + protected ConcurrentHashMap<BGPId, BGPPeer> connectedPeers = new ConcurrentHashMap<BGPId, BGPPeer>(); | ||
| 46 | 55 | ||
| 56 | + protected BGPPeerManager peerManager = new BGPPeerManager(); | ||
| 47 | final Controller ctrl = new Controller(this); | 57 | final Controller ctrl = new Controller(this); |
| 48 | 58 | ||
| 49 | private BGPConfig bgpconfig = new BGPConfig(); | 59 | private BGPConfig bgpconfig = new BGPConfig(); |
| ... | @@ -57,11 +67,22 @@ public class BGPControllerImpl implements BGPController { | ... | @@ -57,11 +67,22 @@ public class BGPControllerImpl implements BGPController { |
| 57 | @Deactivate | 67 | @Deactivate |
| 58 | public void deactivate() { | 68 | public void deactivate() { |
| 59 | // Close all connected peers | 69 | // Close all connected peers |
| 70 | + closeConnectedPeers(); | ||
| 60 | this.ctrl.stop(); | 71 | this.ctrl.stop(); |
| 61 | log.info("Stopped"); | 72 | log.info("Stopped"); |
| 62 | } | 73 | } |
| 63 | 74 | ||
| 64 | @Override | 75 | @Override |
| 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 | ||
| 65 | public void writeMsg(BGPId bgpId, BGPMessage msg) { | 86 | public void writeMsg(BGPId bgpId, BGPMessage msg) { |
| 66 | // TODO: Send message | 87 | // TODO: Send message |
| 67 | } | 88 | } |
| ... | @@ -88,17 +109,167 @@ public class BGPControllerImpl implements BGPController { | ... | @@ -88,17 +109,167 @@ public class BGPControllerImpl implements BGPController { |
| 88 | } | 109 | } |
| 89 | } | 110 | } |
| 90 | 111 | ||
| 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 | + | ||
| 91 | /** | 121 | /** |
| 92 | - * Get controller instance. | 122 | + * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which |
| 93 | - * | 123 | + * they are. |
| 94 | - * @return ctrl the controller. | ||
| 95 | */ | 124 | */ |
| 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 | + */ | ||
| 96 | public Controller getController() { | 244 | public Controller getController() { |
| 97 | return ctrl; | 245 | return ctrl; |
| 98 | } | 246 | } |
| 99 | 247 | ||
| 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 | + | ||
| 100 | @Override | 266 | @Override |
| 101 | public BGPCfg getConfig() { | 267 | public BGPCfg getConfig() { |
| 102 | return this.bgpconfig; | 268 | return this.bgpconfig; |
| 103 | } | 269 | } |
| 270 | + | ||
| 271 | + @Override | ||
| 272 | + public int getBGPConnNumber() { | ||
| 273 | + return connectedPeers.size(); | ||
| 274 | + } | ||
| 104 | } | 275 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| 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 java.util.Timer; | ||
| 20 | +import java.util.TimerTask; | ||
| 21 | + | ||
| 22 | +import org.slf4j.Logger; | ||
| 23 | +import org.slf4j.LoggerFactory; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * Implement sending keepalive message to connected peer periodically based on negotiated holdtime. | ||
| 27 | + */ | ||
| 28 | +public class BGPKeepAliveTimer { | ||
| 29 | + | ||
| 30 | + private Timer keepAliveTimer; | ||
| 31 | + private BGPChannelHandler handler; | ||
| 32 | + private static final Logger log = LoggerFactory.getLogger(BGPKeepAliveTimer.class); | ||
| 33 | + | ||
| 34 | + /** | ||
| 35 | + * Gets keepalive timer object. | ||
| 36 | + * | ||
| 37 | + * @return keepAliveTimer keepalive timer. | ||
| 38 | + */ | ||
| 39 | + public Timer getKeepAliveTimer() { | ||
| 40 | + return keepAliveTimer; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + /** | ||
| 44 | + * Initialize timer to send keepalive message periodically. | ||
| 45 | + * | ||
| 46 | + * @param h channel handler | ||
| 47 | + * @param seconds time interval. | ||
| 48 | + */ | ||
| 49 | + public BGPKeepAliveTimer(BGPChannelHandler h, int seconds) { | ||
| 50 | + this.handler = h; | ||
| 51 | + this.keepAliveTimer = new Timer(); | ||
| 52 | + this.keepAliveTimer.schedule(new SendKeepAlive(), 0, seconds * 1000); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + /** | ||
| 56 | + * Send keepalive message to connected peer on schedule. | ||
| 57 | + */ | ||
| 58 | + class SendKeepAlive extends TimerTask { | ||
| 59 | + @Override | ||
| 60 | + public void run() { | ||
| 61 | + log.debug("Sending periodic KeepAlive"); | ||
| 62 | + | ||
| 63 | + try { | ||
| 64 | + // Send keep alive message | ||
| 65 | + handler.sendKeepAliveMessage(); | ||
| 66 | + handler.getBgpPacketStats().addOutPacket(); | ||
| 67 | + } catch (Exception e) { | ||
| 68 | + log.info("Exception occured while sending keepAlive message" + e.toString()); | ||
| 69 | + } | ||
| 70 | + } | ||
| 71 | + } | ||
| 72 | +} |
| ... | @@ -43,8 +43,7 @@ public class BGPPacketStatsImpl implements BGPPacketStats { | ... | @@ -43,8 +43,7 @@ public class BGPPacketStatsImpl implements BGPPacketStats { |
| 43 | /** | 43 | /** |
| 44 | * Get the outgoing packet count number. | 44 | * Get the outgoing packet count number. |
| 45 | * | 45 | * |
| 46 | - * @return | 46 | + * @return packet count |
| 47 | - * packet count | ||
| 48 | */ | 47 | */ |
| 49 | public int outPacketCount() { | 48 | public int outPacketCount() { |
| 50 | return outPacketCount; | 49 | return outPacketCount; |
| ... | @@ -53,8 +52,7 @@ public class BGPPacketStatsImpl implements BGPPacketStats { | ... | @@ -53,8 +52,7 @@ public class BGPPacketStatsImpl implements BGPPacketStats { |
| 53 | /** | 52 | /** |
| 54 | * Get the incoming packet count number. | 53 | * Get the incoming packet count number. |
| 55 | * | 54 | * |
| 56 | - * @return | 55 | + * @return packet count |
| 57 | - * packet count | ||
| 58 | */ | 56 | */ |
| 59 | public int inPacketCount() { | 57 | public int inPacketCount() { |
| 60 | return inPacketCount; | 58 | return inPacketCount; |
| ... | @@ -63,8 +61,7 @@ public class BGPPacketStatsImpl implements BGPPacketStats { | ... | @@ -63,8 +61,7 @@ public class BGPPacketStatsImpl implements BGPPacketStats { |
| 63 | /** | 61 | /** |
| 64 | * Get the wrong packet count number. | 62 | * Get the wrong packet count number. |
| 65 | * | 63 | * |
| 66 | - * @return | 64 | + * @return packet count |
| 67 | - * packet count | ||
| 68 | */ | 65 | */ |
| 69 | public int wrongPacketCount() { | 66 | public int wrongPacketCount() { |
| 70 | return wrongPacketCount; | 67 | return wrongPacketCount; |
| ... | @@ -110,8 +107,7 @@ public class BGPPacketStatsImpl implements BGPPacketStats { | ... | @@ -110,8 +107,7 @@ public class BGPPacketStatsImpl implements BGPPacketStats { |
| 110 | /** | 107 | /** |
| 111 | * Get the time. | 108 | * Get the time. |
| 112 | * | 109 | * |
| 113 | - * @return | 110 | + * @return time |
| 114 | - * time | ||
| 115 | */ | 111 | */ |
| 116 | public long getTime() { | 112 | public long getTime() { |
| 117 | return this.time; | 113 | return this.time; | ... | ... |
| 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 java.net.InetSocketAddress; | ||
| 20 | +import java.net.SocketAddress; | ||
| 21 | +import java.util.Collections; | ||
| 22 | +import java.util.List; | ||
| 23 | +import java.util.concurrent.RejectedExecutionException; | ||
| 24 | + | ||
| 25 | +import org.jboss.netty.channel.Channel; | ||
| 26 | +import org.onlab.packet.IpAddress; | ||
| 27 | +import org.onosproject.bgp.controller.BGPId; | ||
| 28 | +import org.onosproject.bgp.controller.BGPPacketStats; | ||
| 29 | +import org.onosproject.bgp.controller.BGPPeer; | ||
| 30 | +import org.onosproject.bgpio.protocol.BGPMessage; | ||
| 31 | +import org.onosproject.bgpio.protocol.BGPVersion; | ||
| 32 | +import org.slf4j.Logger; | ||
| 33 | +import org.slf4j.LoggerFactory; | ||
| 34 | + | ||
| 35 | +import com.google.common.base.MoreObjects; | ||
| 36 | + | ||
| 37 | +/** | ||
| 38 | + * BGPPeerImpl implements BGPPeer, maintains peer information and store updates in RIB . | ||
| 39 | + */ | ||
| 40 | +public class BGPPeerImpl implements BGPPeer { | ||
| 41 | + | ||
| 42 | + protected final Logger log = LoggerFactory.getLogger(BGPPeerImpl.class); | ||
| 43 | + | ||
| 44 | + private static final String SHUTDOWN_MSG = "Worker has already been shutdown"; | ||
| 45 | + | ||
| 46 | + private Channel channel; | ||
| 47 | + protected String channelId; | ||
| 48 | + private boolean connected; | ||
| 49 | + protected boolean isHandShakeComplete = false; | ||
| 50 | + public BGPSessionInfo sessionInfo; | ||
| 51 | + private BGPPacketStatsImpl pktStats; | ||
| 52 | + | ||
| 53 | + @Override | ||
| 54 | + public void init(BGPId bgpId, BGPVersion bgpVersion, BGPPacketStats pktStats) { | ||
| 55 | + this.sessionInfo.setRemoteBgpId(bgpId); | ||
| 56 | + this.sessionInfo.setRemoteBgpVersion(bgpVersion); | ||
| 57 | + this.pktStats = (BGPPacketStatsImpl) pktStats; | ||
| 58 | + this.sessionInfo = new BGPSessionInfo(); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + // ************************ | ||
| 62 | + // Channel related | ||
| 63 | + // ************************ | ||
| 64 | + | ||
| 65 | + @Override | ||
| 66 | + public final void disconnectPeer() { | ||
| 67 | + this.channel.close(); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + @Override | ||
| 71 | + public final void sendMessage(BGPMessage m) { | ||
| 72 | + log.debug("Sending message to {}", channel.getRemoteAddress()); | ||
| 73 | + try { | ||
| 74 | + channel.write(Collections.singletonList(m)); | ||
| 75 | + this.pktStats.addOutPacket(); | ||
| 76 | + } catch (RejectedExecutionException e) { | ||
| 77 | + log.warn(e.getMessage()); | ||
| 78 | + if (!e.getMessage().contains(SHUTDOWN_MSG)) { | ||
| 79 | + throw e; | ||
| 80 | + } | ||
| 81 | + } | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + @Override | ||
| 85 | + public final void sendMessage(List<BGPMessage> msgs) { | ||
| 86 | + try { | ||
| 87 | + channel.write(msgs); | ||
| 88 | + this.pktStats.addOutPacket(msgs.size()); | ||
| 89 | + } catch (RejectedExecutionException e) { | ||
| 90 | + log.warn(e.getMessage()); | ||
| 91 | + if (!e.getMessage().contains(SHUTDOWN_MSG)) { | ||
| 92 | + throw e; | ||
| 93 | + } | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + @Override | ||
| 98 | + public final boolean isConnected() { | ||
| 99 | + return this.connected; | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + @Override | ||
| 103 | + public final void setConnected(boolean connected) { | ||
| 104 | + this.connected = connected; | ||
| 105 | + }; | ||
| 106 | + | ||
| 107 | + @Override | ||
| 108 | + public final void setChannel(Channel channel) { | ||
| 109 | + this.channel = channel; | ||
| 110 | + final SocketAddress address = channel.getRemoteAddress(); | ||
| 111 | + if (address instanceof InetSocketAddress) { | ||
| 112 | + final InetSocketAddress inetAddress = (InetSocketAddress) address; | ||
| 113 | + final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress()); | ||
| 114 | + if (ipAddress.isIp4()) { | ||
| 115 | + channelId = ipAddress.toString() + ':' + inetAddress.getPort(); | ||
| 116 | + } else { | ||
| 117 | + channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort(); | ||
| 118 | + } | ||
| 119 | + } | ||
| 120 | + }; | ||
| 121 | + | ||
| 122 | + @Override | ||
| 123 | + public final Channel getChannel() { | ||
| 124 | + return this.channel; | ||
| 125 | + }; | ||
| 126 | + | ||
| 127 | + @Override | ||
| 128 | + public String channelId() { | ||
| 129 | + return channelId; | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + // ************************ | ||
| 133 | + // BGP Peer features related | ||
| 134 | + // ************************ | ||
| 135 | + | ||
| 136 | + @Override | ||
| 137 | + public final BGPId getBGPId() { | ||
| 138 | + return this.sessionInfo.getRemoteBgpId(); | ||
| 139 | + }; | ||
| 140 | + | ||
| 141 | + @Override | ||
| 142 | + public final String getStringId() { | ||
| 143 | + return this.sessionInfo.getRemoteBgpId().toString(); | ||
| 144 | + } | ||
| 145 | + | ||
| 146 | + @Override | ||
| 147 | + public final void setBgpPeerVersion(BGPVersion peerVersion) { | ||
| 148 | + this.sessionInfo.setRemoteBgpVersion(peerVersion); | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + @Override | ||
| 152 | + public void setBgpPeerASNum(short peerASNum) { | ||
| 153 | + this.sessionInfo.setRemoteBgpASNum(peerASNum); | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + @Override | ||
| 157 | + public void setBgpPeerHoldTime(short peerHoldTime) { | ||
| 158 | + this.sessionInfo.setRemoteBgpHoldTime(peerHoldTime); | ||
| 159 | + } | ||
| 160 | + | ||
| 161 | + @Override | ||
| 162 | + public void setBgpPeerIdentifier(int peerIdentifier) { | ||
| 163 | + this.sessionInfo.setRemoteBgpIdentifier(peerIdentifier); | ||
| 164 | + } | ||
| 165 | + | ||
| 166 | + @Override | ||
| 167 | + public int getBgpPeerIdentifier() { | ||
| 168 | + return this.sessionInfo.getRemoteBgpIdentifier(); | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + @Override | ||
| 172 | + public int getNegotiatedHoldTime() { | ||
| 173 | + return this.sessionInfo.getNegotiatedholdTime(); | ||
| 174 | + } | ||
| 175 | + | ||
| 176 | + @Override | ||
| 177 | + public void setNegotiatedHoldTime(short negotiatedHoldTime) { | ||
| 178 | + this.sessionInfo.setNegotiatedholdTime(negotiatedHoldTime); | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + @Override | ||
| 182 | + public boolean isHandshakeComplete() { | ||
| 183 | + return isHandShakeComplete; | ||
| 184 | + } | ||
| 185 | + | ||
| 186 | + @Override | ||
| 187 | + public String toString() { | ||
| 188 | + return MoreObjects.toStringHelper(getClass()).omitNullValues().add("channel", channelId()) | ||
| 189 | + .add("bgpId", getBGPId()).toString(); | ||
| 190 | + } | ||
| 191 | +} |
| 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 org.onosproject.bgp.controller.BGPId; | ||
| 20 | +import org.onosproject.bgpio.protocol.BGPVersion; | ||
| 21 | +import org.slf4j.Logger; | ||
| 22 | +import org.slf4j.LoggerFactory; | ||
| 23 | + | ||
| 24 | +/** | ||
| 25 | + * Class maintains BGP peer session info. | ||
| 26 | + */ | ||
| 27 | +public class BGPSessionInfo { | ||
| 28 | + | ||
| 29 | + protected final Logger log = LoggerFactory.getLogger(BGPSessionInfo.class); | ||
| 30 | + private BGPId remoteBgpId; | ||
| 31 | + private BGPVersion remoteBgpVersion; | ||
| 32 | + private short remoteBgpASNum; | ||
| 33 | + private short remoteBgpholdTime; | ||
| 34 | + private int remoteBgpIdentifier; | ||
| 35 | + private short negotiatedholdTime; | ||
| 36 | + | ||
| 37 | + /** | ||
| 38 | + * Gets the negotiated hold time for the session. | ||
| 39 | + * | ||
| 40 | + * @return negotiated hold time. | ||
| 41 | + */ | ||
| 42 | + public short getNegotiatedholdTime() { | ||
| 43 | + return negotiatedholdTime; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + /** | ||
| 47 | + * Sets the negotiated hold time for the session. | ||
| 48 | + * | ||
| 49 | + * @param negotiatedholdTime negotiated hold time. | ||
| 50 | + */ | ||
| 51 | + public void setNegotiatedholdTime(short negotiatedholdTime) { | ||
| 52 | + this.negotiatedholdTime = negotiatedholdTime; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + /** | ||
| 56 | + * Gets the BGP ID of BGP peer. | ||
| 57 | + * | ||
| 58 | + * @return bgp ID. | ||
| 59 | + */ | ||
| 60 | + public BGPId getRemoteBgpId() { | ||
| 61 | + return remoteBgpId; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + /** | ||
| 65 | + * Sets the BGP ID of bgp peer. | ||
| 66 | + * | ||
| 67 | + * @param bgpId BGP ID to set. | ||
| 68 | + */ | ||
| 69 | + public void setRemoteBgpId(BGPId bgpId) { | ||
| 70 | + log.debug("Remote BGP ID {}", bgpId); | ||
| 71 | + this.remoteBgpId = bgpId; | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + /** | ||
| 75 | + * Gets the BGP version of peer. | ||
| 76 | + * | ||
| 77 | + * @return bgp version. | ||
| 78 | + */ | ||
| 79 | + public BGPVersion getRemoteBgpVersion() { | ||
| 80 | + return remoteBgpVersion; | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + /** | ||
| 84 | + * Sets the BGP version for this bgp peer. | ||
| 85 | + * | ||
| 86 | + * @param bgpVersion bgp version to set. | ||
| 87 | + */ | ||
| 88 | + public void setRemoteBgpVersion(BGPVersion bgpVersion) { | ||
| 89 | + log.debug("Remote BGP version {}", bgpVersion); | ||
| 90 | + this.remoteBgpVersion = bgpVersion; | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + /** | ||
| 94 | + * Gets the BGP remote bgp AS number. | ||
| 95 | + * | ||
| 96 | + * @return remoteBgpASNum peer AS number. | ||
| 97 | + */ | ||
| 98 | + public short getRemoteBgpASNum() { | ||
| 99 | + return remoteBgpASNum; | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + /** | ||
| 103 | + * Sets the AS Number for this bgp peer. | ||
| 104 | + * | ||
| 105 | + * @param bgpASNum the autonomous system number value to set. | ||
| 106 | + */ | ||
| 107 | + public void setRemoteBgpASNum(short bgpASNum) { | ||
| 108 | + log.debug("Remote BGP AS number {}", bgpASNum); | ||
| 109 | + this.remoteBgpASNum = bgpASNum; | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + /** | ||
| 113 | + * Gets the BGP peer hold time. | ||
| 114 | + * | ||
| 115 | + * @return bgp hold time. | ||
| 116 | + */ | ||
| 117 | + public short getRemoteBgpHoldTime() { | ||
| 118 | + return remoteBgpholdTime; | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + /** | ||
| 122 | + * Sets the hold time for this bgp peer. | ||
| 123 | + * | ||
| 124 | + * @param holdTime the hold timer value to set. | ||
| 125 | + */ | ||
| 126 | + public void setRemoteBgpHoldTime(short holdTime) { | ||
| 127 | + log.debug("Remote BGP HoldTime {}", holdTime); | ||
| 128 | + this.remoteBgpholdTime = holdTime; | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + /** | ||
| 132 | + * Gets the BGP version for this bgp peer. | ||
| 133 | + * | ||
| 134 | + * @return bgp identifier. | ||
| 135 | + */ | ||
| 136 | + public int getRemoteBgpIdentifier() { | ||
| 137 | + return remoteBgpIdentifier; | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + /** | ||
| 141 | + * Sets the peer identifier value. | ||
| 142 | + * | ||
| 143 | + * @param bgpIdentifier the bgp peer identifier value. | ||
| 144 | + */ | ||
| 145 | + public void setRemoteBgpIdentifier(int bgpIdentifier) { | ||
| 146 | + log.debug("Remote BGP Identifier {}", bgpIdentifier); | ||
| 147 | + this.remoteBgpIdentifier = bgpIdentifier; | ||
| 148 | + } | ||
| 149 | +} |
-
Please register or login to post a comment