Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.bgp.controller.impl; |
| 17 | |
| 18 | import static org.onlab.util.Tools.groupedThreads; |
| 19 | |
| 20 | import java.lang.management.ManagementFactory; |
| 21 | import java.lang.management.RuntimeMXBean; |
| 22 | import java.net.InetSocketAddress; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.Map; |
| 25 | import java.util.concurrent.Executors; |
| 26 | |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame^] | 27 | import org.jboss.netty.bootstrap.ClientBootstrap; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 28 | import org.jboss.netty.bootstrap.ServerBootstrap; |
| 29 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 30 | import org.jboss.netty.channel.group.ChannelGroup; |
| 31 | import org.jboss.netty.channel.group.DefaultChannelGroup; |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame^] | 32 | import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 33 | import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 34 | import org.onosproject.bgp.controller.BGPController; |
| 35 | import org.onosproject.bgpio.protocol.BGPFactories; |
| 36 | import org.onosproject.bgpio.protocol.BGPFactory; |
| 37 | import org.onosproject.bgpio.protocol.BGPVersion; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 38 | import org.slf4j.Logger; |
| 39 | import 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 | */ |
| 45 | public class Controller { |
| 46 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 47 | private static final Logger log = LoggerFactory.getLogger(Controller.class); |
| 48 | |
| 49 | private static final BGPFactory FACTORY4 = BGPFactories.getFactory(BGPVersion.BGP_4); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 50 | |
| 51 | private ChannelGroup cg; |
| 52 | |
| 53 | // Configuration options |
| 54 | private static final short BGP_PORT_NUM = 179; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 55 | private final int workerThreads = 16; |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame^] | 56 | private final int peerWorkerThreads = 16; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 57 | |
| 58 | // Start time of the controller |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 59 | private long systemStartTime; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 60 | |
| 61 | private NioServerSocketChannelFactory serverExecFactory; |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame^] | 62 | private NioClientSocketChannelFactory peerExecFactory; |
| 63 | private static ClientBootstrap peerBootstrap; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 64 | private BGPController bgpController; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 65 | |
| 66 | // Perf. related configuration |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 67 | private static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 68 | |
| 69 | /** |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 70 | * Constructor to initialize the values. |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 71 | * |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 72 | * @param bgpController bgp controller instance |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 73 | */ |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 74 | 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 K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 85 | } |
| 86 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 87 | /** |
| 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 K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 96 | /** |
| 97 | * Tell controller that we're ready to accept bgp peer connections. |
| 98 | */ |
| 99 | public void run() { |
| 100 | |
| 101 | try { |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame^] | 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 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 110 | 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 VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 117 | ChannelPipelineFactory pfact = new BGPPipelineFactory(bgpController, true); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 118 | |
| 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 VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame^] | 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 | /** |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 181 | * 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 VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame^] | 189 | /** |
| 190 | * Gets run time memory. |
| 191 | * |
| 192 | * @return m run time memory |
| 193 | */ |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 194 | 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 VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame^] | 202 | /** |
| 203 | * Gets UP time. |
| 204 | * |
| 205 | * @return UP time |
| 206 | */ |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 207 | 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 VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame^] | 227 | peerExecFactory.shutdown(); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 228 | 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 | } |