Shashikanth VH
Committed by Gerrit Code Review

[ONOS-2594] Channel handle to manage session handling with BGP peers

Change-Id: I6c58c674aa17e2a86b063e248af589d524ec5fe3
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
16 16
17 package org.onosproject.bgp.controller; 17 package org.onosproject.bgp.controller;
18 18
19 +import java.util.Map;
20 +
21 +import org.onosproject.bgpio.exceptions.BGPParseException;
19 import org.onosproject.bgpio.protocol.BGPMessage; 22 import org.onosproject.bgpio.protocol.BGPMessage;
20 23
21 /** 24 /**
...@@ -52,8 +55,9 @@ public interface BGPController { ...@@ -52,8 +55,9 @@ public interface BGPController {
52 * 55 *
53 * @param bgpId id of the peer the message arrived on 56 * @param bgpId id of the peer the message arrived on
54 * @param msg the message to process. 57 * @param msg the message to process.
58 + * @throws BGPParseException on data processing error
55 */ 59 */
56 - void processBGPPacket(BGPId bgpId, BGPMessage msg); 60 + void processBGPPacket(BGPId bgpId, BGPMessage msg) throws BGPParseException;
57 61
58 /** 62 /**
59 * Close all connected BGP peers. 63 * Close all connected BGP peers.
...@@ -73,5 +77,19 @@ public interface BGPController { ...@@ -73,5 +77,19 @@ public interface BGPController {
73 * 77 *
74 * @return the integer number 78 * @return the integer number
75 */ 79 */
76 - int getBGPConnNumber(); 80 + int connectedPeerCount();
81 +
82 + /**
83 + * Return BGP peer manager.
84 + *
85 + * @return BGPPeerManager peer manager instance
86 + */
87 + BgpPeerManager peerManager();
88 +
89 + /**
90 + * Return BGP connected peers.
91 + *
92 + * @return connectedPeers connected peers
93 + */
94 + Map<BGPId, BGPPeer> connectedPeers();
77 } 95 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -16,30 +16,16 @@ ...@@ -16,30 +16,16 @@
16 package org.onosproject.bgp.controller; 16 package org.onosproject.bgp.controller;
17 import java.util.List; 17 import java.util.List;
18 import org.jboss.netty.channel.Channel; 18 import org.jboss.netty.channel.Channel;
19 +import org.onosproject.bgpio.protocol.BGPFactory;
19 import org.onosproject.bgpio.protocol.BGPMessage; 20 import org.onosproject.bgpio.protocol.BGPMessage;
20 -import org.onosproject.bgpio.protocol.BGPVersion;
21 21
22 /** 22 /**
23 - * Represents the peer side of an bgp peer. 23 + * Represents the peer side of an BGP peer.
24 * 24 *
25 */ 25 */
26 public interface BGPPeer { 26 public interface BGPPeer {
27 27
28 /** 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. 29 * Sets the associated Netty channel for this bgp peer.
44 * 30 *
45 * @param channel the Netty channel 31 * @param channel the Netty channel
...@@ -54,27 +40,6 @@ public interface BGPPeer { ...@@ -54,27 +40,6 @@ public interface BGPPeer {
54 Channel getChannel(); 40 Channel getChannel();
55 41
56 /** 42 /**
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. 43 * Sets whether the bgp peer is connected.
79 * 44 *
80 * @param connected whether the bgp peer is connected 45 * @param connected whether the bgp peer is connected
...@@ -82,15 +47,6 @@ public interface BGPPeer { ...@@ -82,15 +47,6 @@ public interface BGPPeer {
82 void setConnected(boolean connected); 47 void setConnected(boolean connected);
83 48
84 /** 49 /**
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. 50 * Checks whether the handshake is complete.
95 * 51 *
96 * @return true is finished, false if not. 52 * @return true is finished, false if not.
...@@ -112,18 +68,11 @@ public interface BGPPeer { ...@@ -112,18 +68,11 @@ public interface BGPPeer {
112 void sendMessage(List<BGPMessage> msgs); 68 void sendMessage(List<BGPMessage> msgs);
113 69
114 /** 70 /**
115 - * Gets a string version of the ID for this bgp peer. 71 + * Provides the factory for BGP version.
116 * 72 *
117 - * @return string version of the ID 73 + * @return BGP version specific factory.
118 */ 74 */
119 - String getStringId(); 75 + BGPFactory factory();
120 -
121 - /**
122 - * Gets the ipAddress of the peer.
123 - *
124 - * @return the peer bgpId in IPAddress format
125 - */
126 - BGPId getBGPId();
127 76
128 /** 77 /**
129 * Checks if the bgp peer is still connected. 78 * Checks if the bgp peer is still connected.
...@@ -146,16 +95,9 @@ public interface BGPPeer { ...@@ -146,16 +95,9 @@ public interface BGPPeer {
146 String channelId(); 95 String channelId();
147 96
148 /** 97 /**
149 - * Gets the negotiated hold time. 98 + * Return the BGP session info.
150 - *
151 - * @return the negotiated hold time
152 - */
153 - int getNegotiatedHoldTime();
154 -
155 - /**
156 - * Sets negotiated hold time for the peer.
157 * 99 *
158 - * @param negotiatedHoldTime negotiated hold time 100 + * @return sessionInfo bgp session info
159 */ 101 */
160 - void setNegotiatedHoldTime(short negotiatedHoldTime); 102 + BgpSessionInfo sessionInfo();
161 } 103 }
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5 + * the License. You may obtain a copy of the License at
6 + *
7 + * http://www.apache.org/licenses/LICENSE-2.0
8 + *
9 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10 + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 + * specific language governing permissions and limitations under the License.
12 + */
13 +
14 +package org.onosproject.bgp.controller;
15 +
16 +import org.onosproject.bgpio.protocol.BGPVersion;
17 +
18 +/**
19 + * Abstraction of an BGP session info. Maintian session parameters obtained during session creation.
20 + */
21 +public interface BgpSessionInfo {
22 + /**
23 + * Gets the bgp session type iBGP/eBGP.
24 + *
25 + * @return isiBGPSession, true if session is of type internal, otherwise false.
26 + */
27 + boolean isIbgpSession();
28 +
29 + /**
30 + * Gets the negotiated hold time for the session.
31 + *
32 + * @return negotiated hold time.
33 + */
34 + short negotiatedholdTime();
35 +
36 + /**
37 + * Gets the BGP ID of BGP peer.
38 + *
39 + * @return bgp ID.
40 + */
41 + BGPId remoteBgpId();
42 +
43 + /**
44 + * Gets the BGP version of peer.
45 + *
46 + * @return bgp version.
47 + */
48 + BGPVersion remoteBgpVersion();
49 +
50 + /**
51 + * Gets the BGP remote bgp AS number.
52 + *
53 + * @return remoteBgpASNum peer AS number.
54 + */
55 + long remoteBgpASNum();
56 +
57 + /**
58 + * Gets the BGP peer hold time.
59 + *
60 + * @return bgp hold time.
61 + */
62 + short remoteBgpHoldTime();
63 +
64 + /**
65 + * Gets the BGP version for this bgp peer.
66 + *
67 + * @return bgp identifier.
68 + */
69 + int remoteBgpIdentifier();
70 +}
...@@ -20,6 +20,7 @@ import java.io.IOException; ...@@ -20,6 +20,7 @@ import java.io.IOException;
20 import java.net.InetSocketAddress; 20 import java.net.InetSocketAddress;
21 import java.net.SocketAddress; 21 import java.net.SocketAddress;
22 import java.nio.channels.ClosedChannelException; 22 import java.nio.channels.ClosedChannelException;
23 +import java.util.Collections;
23 import java.util.Date; 24 import java.util.Date;
24 import java.util.List; 25 import java.util.List;
25 import java.util.concurrent.RejectedExecutionException; 26 import java.util.concurrent.RejectedExecutionException;
...@@ -32,15 +33,18 @@ import org.jboss.netty.channel.MessageEvent; ...@@ -32,15 +33,18 @@ import org.jboss.netty.channel.MessageEvent;
32 import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler; 33 import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler;
33 import org.jboss.netty.handler.timeout.ReadTimeoutException; 34 import org.jboss.netty.handler.timeout.ReadTimeoutException;
34 import org.jboss.netty.handler.timeout.ReadTimeoutHandler; 35 import org.jboss.netty.handler.timeout.ReadTimeoutHandler;
36 +import org.onlab.packet.Ip4Address;
35 import org.onlab.packet.IpAddress; 37 import org.onlab.packet.IpAddress;
36 import org.onosproject.bgp.controller.BGPCfg; 38 import org.onosproject.bgp.controller.BGPCfg;
39 +import org.onosproject.bgp.controller.BGPController;
37 import org.onosproject.bgp.controller.BGPId; 40 import org.onosproject.bgp.controller.BGPId;
38 import org.onosproject.bgp.controller.BGPPeer; 41 import org.onosproject.bgp.controller.BGPPeer;
39 import org.onosproject.bgp.controller.BGPPeerCfg; 42 import org.onosproject.bgp.controller.BGPPeerCfg;
40 -import org.onosproject.bgp.controller.impl.BGPControllerImpl.BGPPeerManager; 43 +import org.onosproject.bgp.controller.impl.BGPControllerImpl.BGPPeerManagerImpl;
41 import org.onosproject.bgpio.exceptions.BGPParseException; 44 import org.onosproject.bgpio.exceptions.BGPParseException;
45 +import org.onosproject.bgpio.protocol.BGPFactory;
42 import org.onosproject.bgpio.protocol.BGPMessage; 46 import org.onosproject.bgpio.protocol.BGPMessage;
43 -//import org.onosproject.bgpio.protocol.BGPOpenMsg; 47 +import org.onosproject.bgpio.protocol.BGPOpenMsg;
44 import org.onosproject.bgpio.protocol.BGPType; 48 import org.onosproject.bgpio.protocol.BGPType;
45 import org.onosproject.bgpio.protocol.BGPVersion; 49 import org.onosproject.bgpio.protocol.BGPVersion;
46 import org.slf4j.Logger; 50 import org.slf4j.Logger;
...@@ -56,11 +60,11 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -56,11 +60,11 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
56 static final int BGP_MAX_KEEPALIVE_INTERVAL = 3; 60 static final int BGP_MAX_KEEPALIVE_INTERVAL = 3;
57 private BGPPeer bgpPeer; 61 private BGPPeer bgpPeer;
58 private BGPId thisbgpId; 62 private BGPId thisbgpId;
59 - Channel channel; 63 + private Channel channel;
60 private BGPKeepAliveTimer keepAliveTimer = null; 64 private BGPKeepAliveTimer keepAliveTimer = null;
61 private short peerHoldTime = 0; 65 private short peerHoldTime = 0;
62 private short negotiatedHoldTime = 0; 66 private short negotiatedHoldTime = 0;
63 - private short peerAsNum; 67 + private long peerAsNum;
64 private int peerIdentifier; 68 private int peerIdentifier;
65 private BGPPacketStatsImpl bgpPacketStats; 69 private BGPPacketStatsImpl bgpPacketStats;
66 static final int MAX_WRONG_COUNT_PACKET = 5; 70 static final int MAX_WRONG_COUNT_PACKET = 5;
...@@ -72,13 +76,17 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -72,13 +76,17 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
72 // When a bgp peer with a ip addresss is found (i.e we already have a 76 // When a bgp peer with a ip addresss is found (i.e we already have a
73 // connected peer with the same ip), the new peer is immediately 77 // connected peer with the same ip), the new peer is immediately
74 // disconnected. At that point netty callsback channelDisconnected() which 78 // disconnected. At that point netty callsback channelDisconnected() which
75 - // proceeds to cleaup peer state - we need to ensure that it does not cleanup 79 + // proceeds to cleaup peer state - we need to ensure that it does not
80 + // cleanup
76 // peer state for the older (still connected) peer 81 // peer state for the older (still connected) peer
77 private volatile Boolean duplicateBGPIdFound; 82 private volatile Boolean duplicateBGPIdFound;
78 // Indicates the bgp version used by this bgp peer 83 // Indicates the bgp version used by this bgp peer
79 protected BGPVersion bgpVersion; 84 protected BGPVersion bgpVersion;
80 - private BGPControllerImpl bgpControllerImpl; 85 + private BGPController bgpController;
81 - private BGPPeerManager peerManager; 86 + protected BGPFactory factory4;
87 + private boolean isIbgpSession;
88 + private BgpSessionInfoImpl sessionInfo;
89 + private BGPPeerManagerImpl peerManager;
82 private InetSocketAddress inetAddress; 90 private InetSocketAddress inetAddress;
83 private IpAddress ipAddress; 91 private IpAddress ipAddress;
84 private SocketAddress address; 92 private SocketAddress address;
...@@ -88,15 +96,16 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -88,15 +96,16 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
88 /** 96 /**
89 * Create a new unconnected BGPChannelHandler. 97 * Create a new unconnected BGPChannelHandler.
90 * 98 *
91 - * @param bgpCtrlImpl bgp controller implementation object 99 + * @param bgpController bgp controller
92 */ 100 */
93 - BGPChannelHandler(BGPControllerImpl bgpCtrlImpl) { 101 + BGPChannelHandler(BGPController bgpController) {
94 - this.bgpControllerImpl = bgpCtrlImpl; 102 + this.bgpController = bgpController;
95 - this.peerManager = bgpCtrlImpl.getPeerManager(); 103 + this.peerManager = (BGPPeerManagerImpl) bgpController.peerManager();
96 this.state = ChannelState.IDLE; 104 this.state = ChannelState.IDLE;
105 + this.factory4 = Controller.getBGPMessageFactory4();
97 this.duplicateBGPIdFound = Boolean.FALSE; 106 this.duplicateBGPIdFound = Boolean.FALSE;
98 this.bgpPacketStats = new BGPPacketStatsImpl(); 107 this.bgpPacketStats = new BGPPacketStatsImpl();
99 - this.bgpconfig = bgpCtrlImpl.getConfig(); 108 + this.bgpconfig = bgpController.getConfig();
100 } 109 }
101 110
102 // To disconnect peer session. 111 // To disconnect peer session.
...@@ -133,11 +142,11 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -133,11 +142,11 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
133 log.debug("Sending keep alive message in OPENSENT state"); 142 log.debug("Sending keep alive message in OPENSENT state");
134 h.bgpPacketStats.addInPacket(); 143 h.bgpPacketStats.addInPacket();
135 144
136 - // TODO: initialize openmessage BGPOpenMsg pOpenmsg = (BGPOpenMsg) m; 145 + BGPOpenMsg pOpenmsg = (BGPOpenMsg) m;
137 - // TODO: initialize identifier from open messgae h.peerIdentifier = pOpenmsg.getBgpId(); 146 + h.peerIdentifier = pOpenmsg.getBgpId();
138 147
139 // validate capabilities and open msg 148 // validate capabilities and open msg
140 - if (h.openMsgValidation(h)) { 149 + if (h.openMsgValidation(h, pOpenmsg)) {
141 log.debug("Sending handshake OPEN message"); 150 log.debug("Sending handshake OPEN message");
142 151
143 /* 152 /*
...@@ -145,7 +154,7 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -145,7 +154,7 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
145 * value of the Hold Timer by using the smaller of its configured Hold Time and the Hold Time 154 * value of the Hold Timer by using the smaller of its configured Hold Time and the Hold Time
146 * received in the OPEN message 155 * received in the OPEN message
147 */ 156 */
148 - // TODO: initialize holdtime from open message h.peerHoldTime = pOpenmsg.getHoldTime(); 157 + h.peerHoldTime = pOpenmsg.getHoldTime();
149 if (h.peerHoldTime < h.bgpconfig.getHoldTime()) { 158 if (h.peerHoldTime < h.bgpconfig.getHoldTime()) {
150 h.channel.getPipeline().replace("holdTime", 159 h.channel.getPipeline().replace("holdTime",
151 "holdTime", 160 "holdTime",
...@@ -155,7 +164,8 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -155,7 +164,8 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
155 164
156 log.info("Hold Time : " + h.peerHoldTime); 165 log.info("Hold Time : " + h.peerHoldTime);
157 166
158 - // TODO: get AS number for open message update AS number 167 + // update AS number
168 + h.peerAsNum = pOpenmsg.getAsNumber();
159 } 169 }
160 170
161 // Send keepalive message to peer. 171 // Send keepalive message to peer.
...@@ -180,10 +190,11 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -180,10 +190,11 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
180 } else { 190 } else {
181 h.bgpPacketStats.addInPacket(); 191 h.bgpPacketStats.addInPacket();
182 192
183 - // TODO: initialize open message BGPOpenMsg pOpenmsg = (BGPOpenMsg) m; 193 + BGPOpenMsg pOpenmsg = (BGPOpenMsg) m;
194 + h.peerIdentifier = pOpenmsg.getBgpId();
184 195
185 // Validate open message 196 // Validate open message
186 - if (h.openMsgValidation(h)) { 197 + if (h.openMsgValidation(h, pOpenmsg)) {
187 log.debug("Sending handshake OPEN message"); 198 log.debug("Sending handshake OPEN message");
188 199
189 /* 200 /*
...@@ -191,7 +202,7 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -191,7 +202,7 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
191 * value of the Hold Timer by using the smaller of its configured Hold Time and the Hold Time 202 * value of the Hold Timer by using the smaller of its configured Hold Time and the Hold Time
192 * received in the OPEN message 203 * received in the OPEN message
193 */ 204 */
194 - // TODO: get hold time from open message h.peerHoldTime = pOpenmsg.getHoldTime(); 205 + h.peerHoldTime = pOpenmsg.getHoldTime();
195 if (h.peerHoldTime < h.bgpconfig.getHoldTime()) { 206 if (h.peerHoldTime < h.bgpconfig.getHoldTime()) {
196 h.channel.getPipeline().replace("holdTime", 207 h.channel.getPipeline().replace("holdTime",
197 "holdTime", 208 "holdTime",
...@@ -201,7 +212,8 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -201,7 +212,8 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
201 212
202 log.debug("Hold Time : " + h.peerHoldTime); 213 log.debug("Hold Time : " + h.peerHoldTime);
203 214
204 - //TODO: update AS number form open messsage update AS number 215 + // update AS number
216 + h.peerAsNum = pOpenmsg.getAsNumber();
205 217
206 h.sendHandshakeOpenMessage(); 218 h.sendHandshakeOpenMessage();
207 h.bgpPacketStats.addOutPacket(); 219 h.bgpPacketStats.addOutPacket();
...@@ -229,20 +241,17 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -229,20 +241,17 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
229 final InetSocketAddress inetAddress = (InetSocketAddress) h.address; 241 final InetSocketAddress inetAddress = (InetSocketAddress) h.address;
230 h.thisbgpId = BGPId.bgpId(IpAddress.valueOf(inetAddress.getAddress())); 242 h.thisbgpId = BGPId.bgpId(IpAddress.valueOf(inetAddress.getAddress()));
231 243
232 - h.bgpPeer = h.peerManager.getBGPPeerInstance(h.thisbgpId, h.bgpVersion, h.bgpPacketStats); 244 + // set session parameters
233 - // set the status fo bgp as connected 245 + h.negotiatedHoldTime = (h.peerHoldTime < h.bgpconfig.getHoldTime()) ? h.peerHoldTime
246 + : h.bgpconfig.getHoldTime();
247 + h.sessionInfo = new BgpSessionInfoImpl(h.thisbgpId, h.bgpVersion, h.peerAsNum, h.peerHoldTime,
248 + h.peerIdentifier, h.negotiatedHoldTime, h.isIbgpSession);
249 +
250 + h.bgpPeer = h.peerManager.getBGPPeerInstance(h.bgpController, h.sessionInfo, h.bgpPacketStats);
251 + // set the status of bgp as connected
234 h.bgpPeer.setConnected(true); 252 h.bgpPeer.setConnected(true);
235 h.bgpPeer.setChannel(h.channel); 253 h.bgpPeer.setChannel(h.channel);
236 254
237 - // set specific parameters to bgp peer
238 - h.bgpPeer.setBgpPeerVersion(h.bgpVersion);
239 - h.bgpPeer.setBgpPeerASNum(h.peerAsNum);
240 - h.bgpPeer.setBgpPeerHoldTime(h.peerHoldTime);
241 - h.bgpPeer.setBgpPeerIdentifier(h.peerIdentifier);
242 -
243 - h.negotiatedHoldTime = (h.peerHoldTime < h.bgpconfig.getHoldTime()) ? h.peerHoldTime : h.bgpconfig
244 - .getHoldTime();
245 - h.bgpPeer.setNegotiatedHoldTime(h.negotiatedHoldTime);
246 /* 255 /*
247 * RFC 4271, When an OPEN message is received, sends a KEEPALIVE message, If the negotiated hold 256 * RFC 4271, When an OPEN message is received, sends a KEEPALIVE message, If the negotiated hold
248 * time value is zero, then the HoldTimer and KeepaliveTimer are not started. A reasonable maximum 257 * time value is zero, then the HoldTimer and KeepaliveTimer are not started. A reasonable maximum
...@@ -251,8 +260,8 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -251,8 +260,8 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
251 h.sendKeepAliveMessage(); 260 h.sendKeepAliveMessage();
252 261
253 if (h.negotiatedHoldTime != 0) { 262 if (h.negotiatedHoldTime != 0) {
254 - h.keepAliveTimer 263 + h.keepAliveTimer = new BGPKeepAliveTimer(h,
255 - = new BGPKeepAliveTimer(h, (h.negotiatedHoldTime / BGP_MAX_KEEPALIVE_INTERVAL)); 264 + (h.negotiatedHoldTime / BGP_MAX_KEEPALIVE_INTERVAL));
256 } 265 }
257 266
258 h.bgpPacketStats.addOutPacket(); 267 h.bgpPacketStats.addOutPacket();
...@@ -360,7 +369,7 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -360,7 +369,7 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
360 } 369 }
361 370
362 // if connection is already established close channel 371 // if connection is already established close channel
363 - if (peerManager.isPeerConnected(peerAddr)) { 372 + if (peerManager.isPeerConnected(BGPId.bgpId(IpAddress.valueOf(peerAddr)))) {
364 log.debug("Duplicate connection received, peer {}", peerAddr); 373 log.debug("Duplicate connection received, peer {}", peerAddr);
365 channel.close(); 374 channel.close();
366 return; 375 return;
...@@ -507,11 +516,12 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -507,11 +516,12 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
507 /** 516 /**
508 * To handle the BGP message. 517 * To handle the BGP message.
509 * 518 *
510 - * @param m BGP message 519 + * @param m bgp message
520 + * @throws BGPParseException throw exception
511 */ 521 */
512 private void dispatchMessage(BGPMessage m) throws BGPParseException { 522 private void dispatchMessage(BGPMessage m) throws BGPParseException {
513 bgpPacketStats.addInPacket(); 523 bgpPacketStats.addInPacket();
514 - bgpControllerImpl.processBGPPacket(thisbgpId, m); 524 + bgpController.processBGPPacket(thisbgpId, m);
515 } 525 }
516 526
517 /** 527 /**
...@@ -557,10 +567,16 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -557,10 +567,16 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
557 /** 567 /**
558 * Send handshake open message to the peer. 568 * Send handshake open message to the peer.
559 * 569 *
560 - * @throws IOException ,BGPParseException 570 + * @throws IOException, BGPParseException
561 */ 571 */
562 private void sendHandshakeOpenMessage() throws IOException, BGPParseException { 572 private void sendHandshakeOpenMessage() throws IOException, BGPParseException {
563 - // TODO: send open message. 573 + int bgpId;
574 +
575 + bgpId = Ip4Address.valueOf(bgpconfig.getRouterId()).toInt();
576 + BGPMessage msg = factory4.openMessageBuilder().setAsNumber((short) bgpconfig.getAsNumber())
577 + .setHoldTime(bgpconfig.getHoldTime()).setBgpId(bgpId).build();
578 + log.debug("Sending open message to {}", channel.getRemoteAddress());
579 + channel.write(Collections.singletonList(msg));
564 580
565 } 581 }
566 582
...@@ -572,7 +588,9 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -572,7 +588,9 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
572 */ 588 */
573 synchronized void sendKeepAliveMessage() throws IOException, BGPParseException { 589 synchronized void sendKeepAliveMessage() throws IOException, BGPParseException {
574 590
575 - // TODO: send keep alive message. 591 + BGPMessage msg = factory4.keepaliveMessageBuilder().build();
592 + log.debug("Sending keepalive message to {}", channel.getRemoteAddress());
593 + channel.write(Collections.singletonList(msg));
576 } 594 }
577 595
578 /** 596 /**
...@@ -621,10 +639,11 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler { ...@@ -621,10 +639,11 @@ class BGPChannelHandler extends IdleStateAwareChannelHandler {
621 * Open message validation. 639 * Open message validation.
622 * 640 *
623 * @param h channel handler 641 * @param h channel handler
642 + * @param pOpenmsg open message
624 * @return true if validation succeed, otherwise false 643 * @return true if validation succeed, otherwise false
625 * @throws BGPParseException when received invalid message 644 * @throws BGPParseException when received invalid message
626 */ 645 */
627 - public boolean openMsgValidation(BGPChannelHandler h) throws BGPParseException { 646 + public boolean openMsgValidation(BGPChannelHandler h, BGPOpenMsg pOpenmsg) throws BGPParseException {
628 // TODO: Open message validation. 647 // TODO: Open message validation.
629 return true; 648 return true;
630 } 649 }
......
...@@ -16,25 +16,21 @@ ...@@ -16,25 +16,21 @@
16 16
17 package org.onosproject.bgp.controller.impl; 17 package org.onosproject.bgp.controller.impl;
18 18
19 -import static org.onlab.util.Tools.groupedThreads;
20 -
21 import java.util.concurrent.ConcurrentHashMap; 19 import java.util.concurrent.ConcurrentHashMap;
22 -import java.util.concurrent.ExecutorService;
23 -import java.util.concurrent.Executors;
24 import java.util.concurrent.locks.Lock; 20 import java.util.concurrent.locks.Lock;
25 import java.util.concurrent.locks.ReentrantLock; 21 import java.util.concurrent.locks.ReentrantLock;
22 +
26 import org.apache.felix.scr.annotations.Activate; 23 import org.apache.felix.scr.annotations.Activate;
27 import org.apache.felix.scr.annotations.Component; 24 import org.apache.felix.scr.annotations.Component;
28 import org.apache.felix.scr.annotations.Deactivate; 25 import org.apache.felix.scr.annotations.Deactivate;
29 import org.apache.felix.scr.annotations.Service; 26 import org.apache.felix.scr.annotations.Service;
30 -import org.onlab.packet.IpAddress;
31 import org.onosproject.bgp.controller.BGPCfg; 27 import org.onosproject.bgp.controller.BGPCfg;
32 import org.onosproject.bgp.controller.BGPController; 28 import org.onosproject.bgp.controller.BGPController;
33 import org.onosproject.bgp.controller.BGPId; 29 import org.onosproject.bgp.controller.BGPId;
34 -import org.onosproject.bgp.controller.BGPPacketStats;
35 import org.onosproject.bgp.controller.BGPPeer; 30 import org.onosproject.bgp.controller.BGPPeer;
31 +import org.onosproject.bgp.controller.BgpPeerManager;
32 +import org.onosproject.bgpio.exceptions.BGPParseException;
36 import org.onosproject.bgpio.protocol.BGPMessage; 33 import org.onosproject.bgpio.protocol.BGPMessage;
37 -import org.onosproject.bgpio.protocol.BGPVersion;
38 import org.slf4j.Logger; 34 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory; 35 import org.slf4j.LoggerFactory;
40 36
...@@ -44,16 +40,9 @@ public class BGPControllerImpl implements BGPController { ...@@ -44,16 +40,9 @@ public class BGPControllerImpl implements BGPController {
44 40
45 private static final Logger log = LoggerFactory.getLogger(BGPControllerImpl.class); 41 private static final Logger log = LoggerFactory.getLogger(BGPControllerImpl.class);
46 42
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",
53 - "event-barrier-%d"));
54 protected ConcurrentHashMap<BGPId, BGPPeer> connectedPeers = new ConcurrentHashMap<BGPId, BGPPeer>(); 43 protected ConcurrentHashMap<BGPId, BGPPeer> connectedPeers = new ConcurrentHashMap<BGPId, BGPPeer>();
55 44
56 - protected BGPPeerManager peerManager = new BGPPeerManager(); 45 + protected BGPPeerManagerImpl peerManager = new BGPPeerManagerImpl();
57 final Controller ctrl = new Controller(this); 46 final Controller ctrl = new Controller(this);
58 47
59 private BGPConfig bgpconfig = new BGPConfig(); 48 private BGPConfig bgpconfig = new BGPConfig();
...@@ -84,11 +73,11 @@ public class BGPControllerImpl implements BGPController { ...@@ -84,11 +73,11 @@ public class BGPControllerImpl implements BGPController {
84 73
85 @Override 74 @Override
86 public void writeMsg(BGPId bgpId, BGPMessage msg) { 75 public void writeMsg(BGPId bgpId, BGPMessage msg) {
87 - // TODO: Send message 76 + this.getPeer(bgpId).sendMessage(msg);
88 } 77 }
89 78
90 @Override 79 @Override
91 - public void processBGPPacket(BGPId bgpId, BGPMessage msg) { 80 + public void processBGPPacket(BGPId bgpId, BGPMessage msg) throws BGPParseException {
92 81
93 switch (msg.getType()) { 82 switch (msg.getType()) {
94 case OPEN: 83 case OPEN:
...@@ -122,18 +111,12 @@ public class BGPControllerImpl implements BGPController { ...@@ -122,18 +111,12 @@ public class BGPControllerImpl implements BGPController {
122 * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which 111 * Implementation of an BGP Peer which is responsible for keeping track of connected peers and the state in which
123 * they are. 112 * they are.
124 */ 113 */
125 - public class BGPPeerManager { 114 + public class BGPPeerManagerImpl implements BgpPeerManager {
126 115
127 - private final Logger log = LoggerFactory.getLogger(BGPPeerManager.class); 116 + private final Logger log = LoggerFactory.getLogger(BGPPeerManagerImpl.class);
128 private final Lock peerLock = new ReentrantLock(); 117 private final Lock peerLock = new ReentrantLock();
129 118
130 - /** 119 + @Override
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) { 120 public boolean addConnectedPeer(BGPId bgpId, BGPPeer bgpPeer) {
138 121
139 if (connectedPeers.get(bgpId) != null) { 122 if (connectedPeers.get(bgpId) != null) {
...@@ -147,119 +130,49 @@ public class BGPControllerImpl implements BGPController { ...@@ -147,119 +130,49 @@ public class BGPControllerImpl implements BGPController {
147 } 130 }
148 } 131 }
149 132
150 - /** 133 + @Override
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) { 134 public boolean isPeerConnected(BGPId bgpId) {
157 if (connectedPeers.get(bgpId) == null) { 135 if (connectedPeers.get(bgpId) == null) {
158 - this.log.error("Trying to activate peer but is not in " + "connected peer: bgpIp {}. Aborting ..", 136 + this.log.error("Is peer connected: bgpIp {}.", bgpId.toString());
159 - bgpId.toString());
160 return false; 137 return false;
161 } 138 }
162 139
163 return true; 140 return true;
164 } 141 }
165 142
166 - /** 143 + @Override
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) { 144 public void removeConnectedPeer(BGPId bgpId) {
193 connectedPeers.remove(bgpId); 145 connectedPeers.remove(bgpId);
194 } 146 }
195 147
196 - /** 148 + @Override
197 - * Clear all state in controller peer maps for a bgp peer that has 149 + public BGPPeer getPeer(BGPId bgpId) {
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); 150 return connectedPeers.get(bgpId);
221 } 151 }
222 152
223 /** 153 /**
224 - * Gets bgp peer instance. 154 + * Gets bgp peer instance.
225 - * 155 + *
226 - * @param bgpId bgp identifier. 156 + * @param bgpController controller instance.
227 - * @param pv bgp version. 157 + * @param sessionInfo bgp session info.
228 - * @param pktStats packet statistics. 158 + * @param pktStats packet statistics.
229 - * @return BGPPeer peer instance. 159 + * @return BGPPeer peer instance.
230 - */ 160 + */
231 - public BGPPeer getBGPPeerInstance(BGPId bgpId, BGPVersion pv, BGPPacketStats pktStats) { 161 + public BGPPeer getBGPPeerInstance(BGPController bgpController, BgpSessionInfoImpl sessionInfo,
232 - BGPPeer bgpPeer = new BGPPeerImpl(); 162 + BGPPacketStatsImpl pktStats) {
233 - bgpPeer.init(bgpId, pv, pktStats); 163 + BGPPeer bgpPeer = new BGPPeerImpl(bgpController, sessionInfo, pktStats);
234 return bgpPeer; 164 return bgpPeer;
235 } 165 }
236 166
237 } 167 }
238 168
239 - /** 169 + @Override
240 - * Gets controller instance. 170 + public ConcurrentHashMap<BGPId, BGPPeer> connectedPeers() {
241 - *
242 - * @return Controller instance.
243 - */
244 - public Controller getController() {
245 - return ctrl;
246 - }
247 -
248 - /**
249 - * Gets connected peers.
250 - *
251 - * @return connectedPeers from connected Peers Map.
252 - */
253 - public ConcurrentHashMap<BGPId, BGPPeer> getConnectedPeers() {
254 return connectedPeers; 171 return connectedPeers;
255 } 172 }
256 173
257 - /** 174 + @Override
258 - * Gets peer manager. 175 + public BGPPeerManagerImpl peerManager() {
259 - *
260 - * @return peerManager.
261 - */
262 - public BGPPeerManager getPeerManager() {
263 return peerManager; 176 return peerManager;
264 } 177 }
265 178
...@@ -269,7 +182,7 @@ public class BGPControllerImpl implements BGPController { ...@@ -269,7 +182,7 @@ public class BGPControllerImpl implements BGPController {
269 } 182 }
270 183
271 @Override 184 @Override
272 - public int getBGPConnNumber() { 185 + public int connectedPeerCount() {
273 return connectedPeers.size(); 186 return connectedPeers.size();
274 } 187 }
275 -}
...\ No newline at end of file ...\ No newline at end of file
188 +}
......
...@@ -24,11 +24,12 @@ import java.util.concurrent.RejectedExecutionException; ...@@ -24,11 +24,12 @@ import java.util.concurrent.RejectedExecutionException;
24 24
25 import org.jboss.netty.channel.Channel; 25 import org.jboss.netty.channel.Channel;
26 import org.onlab.packet.IpAddress; 26 import org.onlab.packet.IpAddress;
27 -import org.onosproject.bgp.controller.BGPId; 27 +import org.onosproject.bgp.controller.BGPController;
28 -import org.onosproject.bgp.controller.BGPPacketStats;
29 import org.onosproject.bgp.controller.BGPPeer; 28 import org.onosproject.bgp.controller.BGPPeer;
29 +import org.onosproject.bgp.controller.BgpSessionInfo;
30 +import org.onosproject.bgpio.protocol.BGPFactories;
31 +import org.onosproject.bgpio.protocol.BGPFactory;
30 import org.onosproject.bgpio.protocol.BGPMessage; 32 import org.onosproject.bgpio.protocol.BGPMessage;
31 -import org.onosproject.bgpio.protocol.BGPVersion;
32 import org.slf4j.Logger; 33 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory; 34 import org.slf4j.LoggerFactory;
34 35
...@@ -43,19 +44,31 @@ public class BGPPeerImpl implements BGPPeer { ...@@ -43,19 +44,31 @@ public class BGPPeerImpl implements BGPPeer {
43 44
44 private static final String SHUTDOWN_MSG = "Worker has already been shutdown"; 45 private static final String SHUTDOWN_MSG = "Worker has already been shutdown";
45 46
47 + private BGPController bgpController;
46 private Channel channel; 48 private Channel channel;
47 protected String channelId; 49 protected String channelId;
48 private boolean connected; 50 private boolean connected;
49 protected boolean isHandShakeComplete = false; 51 protected boolean isHandShakeComplete = false;
50 - public BGPSessionInfo sessionInfo; 52 + private BgpSessionInfo sessionInfo;
51 private BGPPacketStatsImpl pktStats; 53 private BGPPacketStatsImpl pktStats;
52 54
55 +
53 @Override 56 @Override
54 - public void init(BGPId bgpId, BGPVersion bgpVersion, BGPPacketStats pktStats) { 57 + public BgpSessionInfo sessionInfo() {
55 - this.sessionInfo.setRemoteBgpId(bgpId); 58 + return sessionInfo;
56 - this.sessionInfo.setRemoteBgpVersion(bgpVersion); 59 + }
57 - this.pktStats = (BGPPacketStatsImpl) pktStats; 60 +
58 - this.sessionInfo = new BGPSessionInfo(); 61 + /**
62 + * Initialize peer.
63 + *
64 + *@param bgpController controller instance
65 + *@param sessionInfo bgp session info
66 + *@param pktStats packet statistics
67 + */
68 + public BGPPeerImpl(BGPController bgpController, BgpSessionInfo sessionInfo, BGPPacketStatsImpl pktStats) {
69 + this.bgpController = bgpController;
70 + this.sessionInfo = sessionInfo;
71 + this.pktStats = pktStats;
59 } 72 }
60 73
61 // ************************ 74 // ************************
...@@ -129,53 +142,9 @@ public class BGPPeerImpl implements BGPPeer { ...@@ -129,53 +142,9 @@ public class BGPPeerImpl implements BGPPeer {
129 return channelId; 142 return channelId;
130 } 143 }
131 144
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 145 @Override
177 - public void setNegotiatedHoldTime(short negotiatedHoldTime) { 146 + public BGPFactory factory() {
178 - this.sessionInfo.setNegotiatedholdTime(negotiatedHoldTime); 147 + return BGPFactories.getFactory(sessionInfo.remoteBgpVersion());
179 } 148 }
180 149
181 @Override 150 @Override
...@@ -185,7 +154,8 @@ public class BGPPeerImpl implements BGPPeer { ...@@ -185,7 +154,8 @@ public class BGPPeerImpl implements BGPPeer {
185 154
186 @Override 155 @Override
187 public String toString() { 156 public String toString() {
188 - return MoreObjects.toStringHelper(getClass()).omitNullValues().add("channel", channelId()) 157 + return MoreObjects.toStringHelper(getClass()).omitNullValues()
189 - .add("bgpId", getBGPId()).toString(); 158 + .add("channel", channelId())
159 + .add("bgpId", sessionInfo().remoteBgpId()).toString();
190 } 160 }
191 } 161 }
......
...@@ -23,6 +23,7 @@ import org.jboss.netty.handler.timeout.ReadTimeoutHandler; ...@@ -23,6 +23,7 @@ import org.jboss.netty.handler.timeout.ReadTimeoutHandler;
23 import org.jboss.netty.util.ExternalResourceReleasable; 23 import org.jboss.netty.util.ExternalResourceReleasable;
24 import org.jboss.netty.util.HashedWheelTimer; 24 import org.jboss.netty.util.HashedWheelTimer;
25 import org.jboss.netty.util.Timer; 25 import org.jboss.netty.util.Timer;
26 +import org.onosproject.bgp.controller.BGPController;
26 27
27 /** 28 /**
28 * Creates a ChannelPipeline for a server-side bgp channel. 29 * Creates a ChannelPipeline for a server-side bgp channel.
...@@ -32,30 +33,36 @@ public class BGPPipelineFactory ...@@ -32,30 +33,36 @@ public class BGPPipelineFactory
32 33
33 static final Timer TIMER = new HashedWheelTimer(); 34 static final Timer TIMER = new HashedWheelTimer();
34 protected ReadTimeoutHandler readTimeoutHandler; 35 protected ReadTimeoutHandler readTimeoutHandler;
35 - BGPControllerImpl bgpCtrlImpl; 36 + private boolean isBgpServ;
37 + private BGPController bgpController;
36 38
37 /** 39 /**
38 * Constructor to initialize the values. 40 * Constructor to initialize the values.
39 * 41 *
40 - * @param ctrlImpl parent ctrlImpl 42 + * @param bgpController parent controller
41 - * @param isServBgp if it is a server or not 43 + * @param isBgpServ if it is a server or remote peer
42 */ 44 */
43 - public BGPPipelineFactory(BGPControllerImpl ctrlImpl, boolean isServBgp) { 45 + public BGPPipelineFactory(BGPController bgpController, boolean isBgpServ) {
44 super(); 46 super();
45 - bgpCtrlImpl = ctrlImpl; 47 + this.isBgpServ = isBgpServ;
46 - /* hold time*/ 48 + this.bgpController = bgpController;
47 - readTimeoutHandler = new ReadTimeoutHandler(TIMER, bgpCtrlImpl.getConfig().getHoldTime()); 49 + /* hold time */
50 + this.readTimeoutHandler = new ReadTimeoutHandler(TIMER, bgpController.getConfig().getHoldTime());
48 } 51 }
49 52
50 @Override 53 @Override
51 public ChannelPipeline getPipeline() throws Exception { 54 public ChannelPipeline getPipeline() throws Exception {
52 - BGPChannelHandler handler = new BGPChannelHandler(bgpCtrlImpl); 55 + BGPChannelHandler handler = new BGPChannelHandler(bgpController);
53 56
54 ChannelPipeline pipeline = Channels.pipeline(); 57 ChannelPipeline pipeline = Channels.pipeline();
55 pipeline.addLast("bgpmessagedecoder", new BGPMessageDecoder()); 58 pipeline.addLast("bgpmessagedecoder", new BGPMessageDecoder());
56 pipeline.addLast("bgpmessageencoder", new BGPMessageEncoder()); 59 pipeline.addLast("bgpmessageencoder", new BGPMessageEncoder());
57 pipeline.addLast("holdTime", readTimeoutHandler); 60 pipeline.addLast("holdTime", readTimeoutHandler);
58 - pipeline.addLast("PassiveHandler", handler); 61 + if (isBgpServ) {
62 + pipeline.addLast("PassiveHandler", handler);
63 + } else {
64 + pipeline.addLast("ActiveHandler", handler);
65 + }
59 66
60 return pipeline; 67 return pipeline;
61 } 68 }
......
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 -}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5 + * the License. You may obtain a copy of the License at
6 + *
7 + * http://www.apache.org/licenses/LICENSE-2.0
8 + *
9 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10 + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 + * specific language governing permissions and limitations under the License.
12 + */
13 +
14 +package org.onosproject.bgp.controller.impl;
15 +
16 +import org.onosproject.bgp.controller.BGPId;
17 +import org.onosproject.bgp.controller.BgpSessionInfo;
18 +import org.onosproject.bgpio.protocol.BGPVersion;
19 +import org.slf4j.Logger;
20 +import org.slf4j.LoggerFactory;
21 +
22 +/**
23 + * Class maintains BGP peer session info.
24 + */
25 +public class BgpSessionInfoImpl implements BgpSessionInfo {
26 +
27 + protected final Logger log = LoggerFactory.getLogger(BgpSessionInfoImpl.class);
28 + private BGPId remoteBgpId;
29 + private BGPVersion remoteBgpVersion;
30 + private long remoteBgpASNum;
31 + private short remoteBgpholdTime;
32 + private int remoteBgpIdentifier;
33 + private short negotiatedholdTime;
34 + private boolean isIbgpSession;
35 +
36 + /**
37 + * Initialize session info.
38 + *
39 + *@param remoteBgpId remote peer id
40 + *@param remoteBgpVersion remote peer version
41 + *@param remoteBgpASNum remote peer AS number
42 + *@param remoteBgpholdTime remote peer hold time
43 + *@param remoteBgpIdentifier remote peer identifier
44 + *@param negotiatedholdTime negotiated hold time
45 + *@param isIbgpSession session type ibgp/ebgp
46 + */
47 + public BgpSessionInfoImpl(BGPId remoteBgpId, BGPVersion remoteBgpVersion, long remoteBgpASNum,
48 + short remoteBgpholdTime, int remoteBgpIdentifier, short negotiatedholdTime,
49 + boolean isIbgpSession) {
50 + this.remoteBgpId = remoteBgpId;
51 + this.remoteBgpVersion = remoteBgpVersion;
52 + this.remoteBgpASNum = remoteBgpASNum;
53 + this.remoteBgpholdTime = remoteBgpholdTime;
54 + this.remoteBgpIdentifier = remoteBgpIdentifier;
55 + this.negotiatedholdTime = negotiatedholdTime;
56 + this.isIbgpSession = isIbgpSession;
57 + }
58 +
59 + @Override
60 + public boolean isIbgpSession() {
61 + return isIbgpSession;
62 + }
63 +
64 + @Override
65 + public short negotiatedholdTime() {
66 + return negotiatedholdTime;
67 + }
68 +
69 + @Override
70 + public BGPId remoteBgpId() {
71 + return remoteBgpId;
72 + }
73 +
74 + @Override
75 + public BGPVersion remoteBgpVersion() {
76 + return remoteBgpVersion;
77 + }
78 +
79 + @Override
80 + public long remoteBgpASNum() {
81 + return remoteBgpASNum;
82 + }
83 +
84 + @Override
85 + public short remoteBgpHoldTime() {
86 + return remoteBgpholdTime;
87 + }
88 +
89 + @Override
90 + public int remoteBgpIdentifier() {
91 + return remoteBgpIdentifier;
92 + }
93 +}
...@@ -29,6 +29,10 @@ import org.jboss.netty.channel.ChannelPipelineFactory; ...@@ -29,6 +29,10 @@ import org.jboss.netty.channel.ChannelPipelineFactory;
29 import org.jboss.netty.channel.group.ChannelGroup; 29 import org.jboss.netty.channel.group.ChannelGroup;
30 import org.jboss.netty.channel.group.DefaultChannelGroup; 30 import org.jboss.netty.channel.group.DefaultChannelGroup;
31 import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; 31 import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
32 +import org.onosproject.bgp.controller.BGPController;
33 +import org.onosproject.bgpio.protocol.BGPFactories;
34 +import org.onosproject.bgpio.protocol.BGPFactory;
35 +import org.onosproject.bgpio.protocol.BGPVersion;
32 import org.slf4j.Logger; 36 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory; 37 import org.slf4j.LoggerFactory;
34 38
...@@ -38,31 +42,41 @@ import org.slf4j.LoggerFactory; ...@@ -38,31 +42,41 @@ import org.slf4j.LoggerFactory;
38 */ 42 */
39 public class Controller { 43 public class Controller {
40 44
41 - protected static final Logger log = LoggerFactory.getLogger(Controller.class); 45 + private static final Logger log = LoggerFactory.getLogger(Controller.class);
46 +
47 + private static final BGPFactory FACTORY4 = BGPFactories.getFactory(BGPVersion.BGP_4);
42 48
43 private ChannelGroup cg; 49 private ChannelGroup cg;
44 50
45 // Configuration options 51 // Configuration options
46 private static final short BGP_PORT_NUM = 179; 52 private static final short BGP_PORT_NUM = 179;
47 - private int workerThreads = 16; 53 + private final int workerThreads = 16;
48 54
49 // Start time of the controller 55 // Start time of the controller
50 - protected long systemStartTime; 56 + private long systemStartTime;
51 57
52 private NioServerSocketChannelFactory serverExecFactory; 58 private NioServerSocketChannelFactory serverExecFactory;
59 + private BGPController bgpController;
53 60
54 // Perf. related configuration 61 // Perf. related configuration
55 - protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; 62 + private static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024;
56 63
57 - BGPControllerImpl bgpCtrlImpl; 64 + /**
65 + * Constructor to initialize the values.
66 + *
67 + * @param bgpController bgp controller instance
68 + */
69 + public Controller(BGPController bgpController) {
70 + this.bgpController = bgpController;
71 + }
58 72
59 /** 73 /**
60 - * Constructor to initialize parameter. 74 + * Returns factory version for processing BGP messages.
61 * 75 *
62 - * @param bgpCtrlImpl BGP controller Impl instance 76 + * @return instance of factory version
63 */ 77 */
64 - public Controller(BGPControllerImpl bgpCtrlImpl) { 78 + static BGPFactory getBGPMessageFactory4() {
65 - this.bgpCtrlImpl = bgpCtrlImpl; 79 + return FACTORY4;
66 } 80 }
67 81
68 // *************** 82 // ***************
...@@ -95,7 +109,7 @@ public class Controller { ...@@ -95,7 +109,7 @@ public class Controller {
95 bootstrap.setOption("child.tcpNoDelay", true); 109 bootstrap.setOption("child.tcpNoDelay", true);
96 bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); 110 bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);
97 111
98 - ChannelPipelineFactory pfact = new BGPPipelineFactory(bgpCtrlImpl, true); 112 + ChannelPipelineFactory pfact = new BGPPipelineFactory(bgpController, true);
99 113
100 bootstrap.setPipelineFactory(pfact); 114 bootstrap.setPipelineFactory(pfact);
101 InetSocketAddress sa = new InetSocketAddress(getBgpPortNum()); 115 InetSocketAddress sa = new InetSocketAddress(getBgpPortNum());
......