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; |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 29 | import org.jboss.netty.channel.Channel; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 30 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 31 | import org.jboss.netty.channel.group.ChannelGroup; |
| 32 | import org.jboss.netty.channel.group.DefaultChannelGroup; |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 33 | import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 34 | import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 35 | import org.onosproject.bgp.controller.BgpController; |
| 36 | import org.onosproject.bgpio.protocol.BgpFactories; |
| 37 | import org.onosproject.bgpio.protocol.BgpFactory; |
| 38 | import org.onosproject.bgpio.protocol.BgpVersion; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 39 | import org.slf4j.Logger; |
| 40 | import org.slf4j.LoggerFactory; |
| 41 | |
| 42 | /** |
| 43 | * The main controller class. Handles all setup and network listeners - Distributed ownership control of bgp peer |
| 44 | * through IControllerRegistryService |
| 45 | */ |
| 46 | public class Controller { |
| 47 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 48 | private static final Logger log = LoggerFactory.getLogger(Controller.class); |
| 49 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 50 | private static final BgpFactory FACTORY4 = BgpFactories.getFactory(BgpVersion.BGP_4); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 51 | |
| 52 | private ChannelGroup cg; |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 53 | public Channel serverChannel; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 54 | |
| 55 | // Configuration options |
| 56 | private static final short BGP_PORT_NUM = 179; |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 57 | private static final short PORT_NUM_ZERO = 0; |
| 58 | private static boolean isPortNumSet = false; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 59 | private final int workerThreads = 16; |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 60 | private final int peerWorkerThreads = 16; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 61 | |
| 62 | // Start time of the controller |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 63 | private long systemStartTime; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 64 | |
| 65 | private NioServerSocketChannelFactory serverExecFactory; |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 66 | private NioClientSocketChannelFactory peerExecFactory; |
| 67 | private static ClientBootstrap peerBootstrap; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 68 | private BgpController bgpController; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 69 | |
| 70 | // Perf. related configuration |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 71 | private static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 72 | |
| 73 | /** |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 74 | * Constructor to initialize the values. |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 75 | * |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 76 | * @param bgpController bgp controller instance |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 77 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 78 | public Controller(BgpController bgpController) { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 79 | this.bgpController = bgpController; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns factory version for processing BGP messages. |
| 84 | * |
| 85 | * @return instance of factory version |
| 86 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 87 | static BgpFactory getBgpMessageFactory4() { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 88 | return FACTORY4; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 89 | } |
| 90 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 91 | /** |
| 92 | * To get system start time. |
| 93 | * |
| 94 | * @return system start time in milliseconds |
| 95 | */ |
| 96 | public long getSystemStartTime() { |
| 97 | return (this.systemStartTime); |
| 98 | } |
| 99 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 100 | /** |
| 101 | * Tell controller that we're ready to accept bgp peer connections. |
| 102 | */ |
| 103 | public void run() { |
| 104 | |
| 105 | try { |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 106 | |
| 107 | peerBootstrap = createPeerBootStrap(); |
| 108 | |
| 109 | peerBootstrap.setOption("reuseAddr", true); |
| 110 | peerBootstrap.setOption("child.keepAlive", true); |
| 111 | peerBootstrap.setOption("child.tcpNoDelay", true); |
| 112 | peerBootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); |
| 113 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 114 | final ServerBootstrap bootstrap = createServerBootStrap(); |
| 115 | |
| 116 | bootstrap.setOption("reuseAddr", true); |
| 117 | bootstrap.setOption("child.keepAlive", true); |
| 118 | bootstrap.setOption("child.tcpNoDelay", true); |
| 119 | bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); |
| 120 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 121 | ChannelPipelineFactory pfact = new BgpPipelineFactory(bgpController, true); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 122 | |
| 123 | bootstrap.setPipelineFactory(pfact); |
| 124 | InetSocketAddress sa = new InetSocketAddress(getBgpPortNum()); |
| 125 | cg = new DefaultChannelGroup(); |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 126 | serverChannel = bootstrap.bind(sa); |
| 127 | cg.add(serverChannel); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 128 | log.info("Listening for Peer connection on {}", sa); |
| 129 | } catch (Exception e) { |
| 130 | throw new RuntimeException(e); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Creates server boot strap. |
| 136 | * |
| 137 | * @return ServerBootStrap |
| 138 | */ |
| 139 | private ServerBootstrap createServerBootStrap() { |
| 140 | |
| 141 | if (workerThreads == 0) { |
| 142 | serverExecFactory = new NioServerSocketChannelFactory( |
| 143 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")), |
| 144 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d"))); |
| 145 | return new ServerBootstrap(serverExecFactory); |
| 146 | } else { |
| 147 | serverExecFactory = new NioServerSocketChannelFactory( |
| 148 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")), |
| 149 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d")), |
| 150 | workerThreads); |
| 151 | return new ServerBootstrap(serverExecFactory); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 156 | * Creates peer boot strap. |
| 157 | * |
| 158 | * @return ClientBootstrap |
| 159 | */ |
| 160 | private ClientBootstrap createPeerBootStrap() { |
| 161 | |
| 162 | if (peerWorkerThreads == 0) { |
| 163 | peerExecFactory = new NioClientSocketChannelFactory( |
| 164 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")), |
| 165 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d"))); |
| 166 | return new ClientBootstrap(peerExecFactory); |
| 167 | } else { |
| 168 | peerExecFactory = new NioClientSocketChannelFactory( |
| 169 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")), |
| 170 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d")), |
| 171 | peerWorkerThreads); |
| 172 | return new ClientBootstrap(peerExecFactory); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Gets peer bootstrap. |
| 178 | * |
| 179 | * @return peer bootstrap |
| 180 | */ |
| 181 | public static ClientBootstrap peerBootstrap() { |
| 182 | return peerBootstrap; |
| 183 | } |
| 184 | |
| 185 | /** |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 186 | * Initialize internal data structures. |
| 187 | */ |
| 188 | public void init() { |
| 189 | // These data structures are initialized here because other |
| 190 | // module's startUp() might be called before ours |
| 191 | this.systemStartTime = System.currentTimeMillis(); |
| 192 | } |
| 193 | |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 194 | /** |
| 195 | * Gets run time memory. |
| 196 | * |
| 197 | * @return m run time memory |
| 198 | */ |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 199 | public Map<String, Long> getMemory() { |
| 200 | Map<String, Long> m = new HashMap<>(); |
| 201 | Runtime runtime = Runtime.getRuntime(); |
| 202 | m.put("total", runtime.totalMemory()); |
| 203 | m.put("free", runtime.freeMemory()); |
| 204 | return m; |
| 205 | } |
| 206 | |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 207 | /** |
| 208 | * Gets UP time. |
| 209 | * |
| 210 | * @return UP time |
| 211 | */ |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 212 | public Long getUptime() { |
| 213 | RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); |
| 214 | return rb.getUptime(); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Starts the BGP controller. |
| 219 | */ |
| 220 | public void start() { |
| 221 | log.info("Started"); |
| 222 | this.init(); |
| 223 | this.run(); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Stops the BGP controller. |
| 228 | */ |
| 229 | public void stop() { |
| 230 | log.info("Stopped"); |
| 231 | serverExecFactory.shutdown(); |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 232 | peerExecFactory.shutdown(); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 233 | cg.close(); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Returns port number. |
| 238 | * |
| 239 | * @return port number |
| 240 | */ |
| 241 | public static short getBgpPortNum() { |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 242 | if (isPortNumSet) { |
| 243 | return PORT_NUM_ZERO; |
| 244 | } |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 245 | return BGP_PORT_NUM; |
| 246 | } |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 247 | |
| 248 | /** |
| 249 | * sets the isPortNumSet as true. |
| 250 | */ |
| 251 | public void setBgpPortNum() { |
| 252 | isPortNumSet = true; |
| 253 | } |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 254 | } |