Shashikanth VH

[ONOS-2592] bgp Client bootstrap to initiate peer connection

Change-Id: I8e1a723942dca76f8bd23ed17b44cd953a19c97a
...@@ -24,10 +24,12 @@ import java.util.HashMap; ...@@ -24,10 +24,12 @@ import java.util.HashMap;
24 import java.util.Map; 24 import java.util.Map;
25 import java.util.concurrent.Executors; 25 import java.util.concurrent.Executors;
26 26
27 +import org.jboss.netty.bootstrap.ClientBootstrap;
27 import org.jboss.netty.bootstrap.ServerBootstrap; 28 import org.jboss.netty.bootstrap.ServerBootstrap;
28 import org.jboss.netty.channel.ChannelPipelineFactory; 29 import org.jboss.netty.channel.ChannelPipelineFactory;
29 import org.jboss.netty.channel.group.ChannelGroup; 30 import org.jboss.netty.channel.group.ChannelGroup;
30 import org.jboss.netty.channel.group.DefaultChannelGroup; 31 import org.jboss.netty.channel.group.DefaultChannelGroup;
32 +import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
31 import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; 33 import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
32 import org.onosproject.bgp.controller.BGPController; 34 import org.onosproject.bgp.controller.BGPController;
33 import org.onosproject.bgpio.protocol.BGPFactories; 35 import org.onosproject.bgpio.protocol.BGPFactories;
...@@ -51,11 +53,14 @@ public class Controller { ...@@ -51,11 +53,14 @@ public class Controller {
51 // Configuration options 53 // Configuration options
52 private static final short BGP_PORT_NUM = 179; 54 private static final short BGP_PORT_NUM = 179;
53 private final int workerThreads = 16; 55 private final int workerThreads = 16;
56 + private final int peerWorkerThreads = 16;
54 57
55 // Start time of the controller 58 // Start time of the controller
56 private long systemStartTime; 59 private long systemStartTime;
57 60
58 private NioServerSocketChannelFactory serverExecFactory; 61 private NioServerSocketChannelFactory serverExecFactory;
62 + private NioClientSocketChannelFactory peerExecFactory;
63 + private static ClientBootstrap peerBootstrap;
59 private BGPController bgpController; 64 private BGPController bgpController;
60 65
61 // Perf. related configuration 66 // Perf. related configuration
...@@ -79,10 +84,6 @@ public class Controller { ...@@ -79,10 +84,6 @@ public class Controller {
79 return FACTORY4; 84 return FACTORY4;
80 } 85 }
81 86
82 - // ***************
83 - // Getters/Setters
84 - // ***************
85 -
86 /** 87 /**
87 * To get system start time. 88 * To get system start time.
88 * 89 *
...@@ -92,16 +93,20 @@ public class Controller { ...@@ -92,16 +93,20 @@ public class Controller {
92 return (this.systemStartTime); 93 return (this.systemStartTime);
93 } 94 }
94 95
95 - // **************
96 - // Initialization
97 - // **************
98 -
99 /** 96 /**
100 * Tell controller that we're ready to accept bgp peer connections. 97 * Tell controller that we're ready to accept bgp peer connections.
101 */ 98 */
102 public void run() { 99 public void run() {
103 100
104 try { 101 try {
102 +
103 + peerBootstrap = createPeerBootStrap();
104 +
105 + peerBootstrap.setOption("reuseAddr", true);
106 + peerBootstrap.setOption("child.keepAlive", true);
107 + peerBootstrap.setOption("child.tcpNoDelay", true);
108 + peerBootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);
109 +
105 final ServerBootstrap bootstrap = createServerBootStrap(); 110 final ServerBootstrap bootstrap = createServerBootStrap();
106 111
107 bootstrap.setOption("reuseAddr", true); 112 bootstrap.setOption("reuseAddr", true);
...@@ -143,6 +148,36 @@ public class Controller { ...@@ -143,6 +148,36 @@ public class Controller {
143 } 148 }
144 149
145 /** 150 /**
151 + * Creates peer boot strap.
152 + *
153 + * @return ClientBootstrap
154 + */
155 + private ClientBootstrap createPeerBootStrap() {
156 +
157 + if (peerWorkerThreads == 0) {
158 + peerExecFactory = new NioClientSocketChannelFactory(
159 + Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")),
160 + Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d")));
161 + return new ClientBootstrap(peerExecFactory);
162 + } else {
163 + peerExecFactory = new NioClientSocketChannelFactory(
164 + Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")),
165 + Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d")),
166 + peerWorkerThreads);
167 + return new ClientBootstrap(peerExecFactory);
168 + }
169 + }
170 +
171 + /**
172 + * Gets peer bootstrap.
173 + *
174 + * @return peer bootstrap
175 + */
176 + public static ClientBootstrap peerBootstrap() {
177 + return peerBootstrap;
178 + }
179 +
180 + /**
146 * Initialize internal data structures. 181 * Initialize internal data structures.
147 */ 182 */
148 public void init() { 183 public void init() {
...@@ -151,10 +186,11 @@ public class Controller { ...@@ -151,10 +186,11 @@ public class Controller {
151 this.systemStartTime = System.currentTimeMillis(); 186 this.systemStartTime = System.currentTimeMillis();
152 } 187 }
153 188
154 - // ************** 189 + /**
155 - // Utility methods 190 + * Gets run time memory.
156 - // ************** 191 + *
157 - 192 + * @return m run time memory
193 + */
158 public Map<String, Long> getMemory() { 194 public Map<String, Long> getMemory() {
159 Map<String, Long> m = new HashMap<>(); 195 Map<String, Long> m = new HashMap<>();
160 Runtime runtime = Runtime.getRuntime(); 196 Runtime runtime = Runtime.getRuntime();
...@@ -163,6 +199,11 @@ public class Controller { ...@@ -163,6 +199,11 @@ public class Controller {
163 return m; 199 return m;
164 } 200 }
165 201
202 + /**
203 + * Gets UP time.
204 + *
205 + * @return UP time
206 + */
166 public Long getUptime() { 207 public Long getUptime() {
167 RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); 208 RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
168 return rb.getUptime(); 209 return rb.getUptime();
...@@ -183,6 +224,7 @@ public class Controller { ...@@ -183,6 +224,7 @@ public class Controller {
183 public void stop() { 224 public void stop() {
184 log.info("Stopped"); 225 log.info("Stopped");
185 serverExecFactory.shutdown(); 226 serverExecFactory.shutdown();
227 + peerExecFactory.shutdown();
186 cg.close(); 228 cg.close();
187 } 229 }
188 230
......