blob: 017c39e53dd8938558a638b1062d6ada95bd85e5 [file] [log] [blame]
Satish Ke107e662015-09-21 19:00:17 +05301/*
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 */
16package org.onosproject.bgp.controller.impl;
17
18import static org.onlab.util.Tools.groupedThreads;
19
20import java.lang.management.ManagementFactory;
21import java.lang.management.RuntimeMXBean;
22import java.net.InetSocketAddress;
23import java.util.HashMap;
24import java.util.Map;
25import java.util.concurrent.Executors;
26
Shashikanth VHfd75b842015-11-18 19:10:13 +053027import org.jboss.netty.bootstrap.ClientBootstrap;
Satish Ke107e662015-09-21 19:00:17 +053028import org.jboss.netty.bootstrap.ServerBootstrap;
29import org.jboss.netty.channel.ChannelPipelineFactory;
30import org.jboss.netty.channel.group.ChannelGroup;
31import org.jboss.netty.channel.group.DefaultChannelGroup;
Shashikanth VHfd75b842015-11-18 19:10:13 +053032import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
Satish Ke107e662015-09-21 19:00:17 +053033import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053034import org.onosproject.bgp.controller.BGPController;
35import org.onosproject.bgpio.protocol.BGPFactories;
36import org.onosproject.bgpio.protocol.BGPFactory;
37import org.onosproject.bgpio.protocol.BGPVersion;
Satish Ke107e662015-09-21 19:00:17 +053038import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
41/**
42 * The main controller class. Handles all setup and network listeners - Distributed ownership control of bgp peer
43 * through IControllerRegistryService
44 */
45public class Controller {
46
Shashikanth VH9f8afb42015-11-04 18:00:30 +053047 private static final Logger log = LoggerFactory.getLogger(Controller.class);
48
49 private static final BGPFactory FACTORY4 = BGPFactories.getFactory(BGPVersion.BGP_4);
Satish Ke107e662015-09-21 19:00:17 +053050
51 private ChannelGroup cg;
52
53 // Configuration options
54 private static final short BGP_PORT_NUM = 179;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053055 private final int workerThreads = 16;
Shashikanth VHfd75b842015-11-18 19:10:13 +053056 private final int peerWorkerThreads = 16;
Satish Ke107e662015-09-21 19:00:17 +053057
58 // Start time of the controller
Shashikanth VH9f8afb42015-11-04 18:00:30 +053059 private long systemStartTime;
Satish Ke107e662015-09-21 19:00:17 +053060
61 private NioServerSocketChannelFactory serverExecFactory;
Shashikanth VHfd75b842015-11-18 19:10:13 +053062 private NioClientSocketChannelFactory peerExecFactory;
63 private static ClientBootstrap peerBootstrap;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053064 private BGPController bgpController;
Satish Ke107e662015-09-21 19:00:17 +053065
66 // Perf. related configuration
Shashikanth VH9f8afb42015-11-04 18:00:30 +053067 private static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024;
Satish Ke107e662015-09-21 19:00:17 +053068
69 /**
Shashikanth VH9f8afb42015-11-04 18:00:30 +053070 * Constructor to initialize the values.
Satish Ke107e662015-09-21 19:00:17 +053071 *
Shashikanth VH9f8afb42015-11-04 18:00:30 +053072 * @param bgpController bgp controller instance
Satish Ke107e662015-09-21 19:00:17 +053073 */
Shashikanth VH9f8afb42015-11-04 18:00:30 +053074 public Controller(BGPController bgpController) {
75 this.bgpController = bgpController;
76 }
77
78 /**
79 * Returns factory version for processing BGP messages.
80 *
81 * @return instance of factory version
82 */
83 static BGPFactory getBGPMessageFactory4() {
84 return FACTORY4;
Satish Ke107e662015-09-21 19:00:17 +053085 }
86
Satish Ke107e662015-09-21 19:00:17 +053087 /**
88 * To get system start time.
89 *
90 * @return system start time in milliseconds
91 */
92 public long getSystemStartTime() {
93 return (this.systemStartTime);
94 }
95
Satish Ke107e662015-09-21 19:00:17 +053096 /**
97 * Tell controller that we're ready to accept bgp peer connections.
98 */
99 public void run() {
100
101 try {
Shashikanth VHfd75b842015-11-18 19:10:13 +0530102
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
Satish Ke107e662015-09-21 19:00:17 +0530110 final ServerBootstrap bootstrap = createServerBootStrap();
111
112 bootstrap.setOption("reuseAddr", true);
113 bootstrap.setOption("child.keepAlive", true);
114 bootstrap.setOption("child.tcpNoDelay", true);
115 bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);
116
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530117 ChannelPipelineFactory pfact = new BGPPipelineFactory(bgpController, true);
Satish Ke107e662015-09-21 19:00:17 +0530118
119 bootstrap.setPipelineFactory(pfact);
120 InetSocketAddress sa = new InetSocketAddress(getBgpPortNum());
121 cg = new DefaultChannelGroup();
122 cg.add(bootstrap.bind(sa));
123 log.info("Listening for Peer connection on {}", sa);
124 } catch (Exception e) {
125 throw new RuntimeException(e);
126 }
127 }
128
129 /**
130 * Creates server boot strap.
131 *
132 * @return ServerBootStrap
133 */
134 private ServerBootstrap createServerBootStrap() {
135
136 if (workerThreads == 0) {
137 serverExecFactory = new NioServerSocketChannelFactory(
138 Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")),
139 Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d")));
140 return new ServerBootstrap(serverExecFactory);
141 } else {
142 serverExecFactory = new NioServerSocketChannelFactory(
143 Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")),
144 Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d")),
145 workerThreads);
146 return new ServerBootstrap(serverExecFactory);
147 }
148 }
149
150 /**
Shashikanth VHfd75b842015-11-18 19:10:13 +0530151 * 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 /**
Satish Ke107e662015-09-21 19:00:17 +0530181 * Initialize internal data structures.
182 */
183 public void init() {
184 // These data structures are initialized here because other
185 // module's startUp() might be called before ours
186 this.systemStartTime = System.currentTimeMillis();
187 }
188
Shashikanth VHfd75b842015-11-18 19:10:13 +0530189 /**
190 * Gets run time memory.
191 *
192 * @return m run time memory
193 */
Satish Ke107e662015-09-21 19:00:17 +0530194 public Map<String, Long> getMemory() {
195 Map<String, Long> m = new HashMap<>();
196 Runtime runtime = Runtime.getRuntime();
197 m.put("total", runtime.totalMemory());
198 m.put("free", runtime.freeMemory());
199 return m;
200 }
201
Shashikanth VHfd75b842015-11-18 19:10:13 +0530202 /**
203 * Gets UP time.
204 *
205 * @return UP time
206 */
Satish Ke107e662015-09-21 19:00:17 +0530207 public Long getUptime() {
208 RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
209 return rb.getUptime();
210 }
211
212 /**
213 * Starts the BGP controller.
214 */
215 public void start() {
216 log.info("Started");
217 this.init();
218 this.run();
219 }
220
221 /**
222 * Stops the BGP controller.
223 */
224 public void stop() {
225 log.info("Stopped");
226 serverExecFactory.shutdown();
Shashikanth VHfd75b842015-11-18 19:10:13 +0530227 peerExecFactory.shutdown();
Satish Ke107e662015-09-21 19:00:17 +0530228 cg.close();
229 }
230
231 /**
232 * Returns port number.
233 *
234 * @return port number
235 */
236 public static short getBgpPortNum() {
237 return BGP_PORT_NUM;
238 }
239}