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 | |
| 27 | import org.jboss.netty.bootstrap.ServerBootstrap; |
| 28 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 29 | import org.jboss.netty.channel.group.ChannelGroup; |
| 30 | import org.jboss.netty.channel.group.DefaultChannelGroup; |
| 31 | import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame^] | 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; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | /** |
| 40 | * The main controller class. Handles all setup and network listeners - Distributed ownership control of bgp peer |
| 41 | * through IControllerRegistryService |
| 42 | */ |
| 43 | public class Controller { |
| 44 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame^] | 45 | private static final Logger log = LoggerFactory.getLogger(Controller.class); |
| 46 | |
| 47 | private static final BGPFactory FACTORY4 = BGPFactories.getFactory(BGPVersion.BGP_4); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 48 | |
| 49 | private ChannelGroup cg; |
| 50 | |
| 51 | // Configuration options |
| 52 | private static final short BGP_PORT_NUM = 179; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame^] | 53 | private final int workerThreads = 16; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 54 | |
| 55 | // Start time of the controller |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame^] | 56 | private long systemStartTime; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 57 | |
| 58 | private NioServerSocketChannelFactory serverExecFactory; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame^] | 59 | private BGPController bgpController; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 60 | |
| 61 | // Perf. related configuration |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame^] | 62 | private static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 63 | |
| 64 | /** |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame^] | 65 | * Constructor to initialize the values. |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 66 | * |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame^] | 67 | * @param bgpController bgp controller instance |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 68 | */ |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame^] | 69 | public Controller(BGPController bgpController) { |
| 70 | this.bgpController = bgpController; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns factory version for processing BGP messages. |
| 75 | * |
| 76 | * @return instance of factory version |
| 77 | */ |
| 78 | static BGPFactory getBGPMessageFactory4() { |
| 79 | return FACTORY4; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // *************** |
| 83 | // Getters/Setters |
| 84 | // *************** |
| 85 | |
| 86 | /** |
| 87 | * To get system start time. |
| 88 | * |
| 89 | * @return system start time in milliseconds |
| 90 | */ |
| 91 | public long getSystemStartTime() { |
| 92 | return (this.systemStartTime); |
| 93 | } |
| 94 | |
| 95 | // ************** |
| 96 | // Initialization |
| 97 | // ************** |
| 98 | |
| 99 | /** |
| 100 | * Tell controller that we're ready to accept bgp peer connections. |
| 101 | */ |
| 102 | public void run() { |
| 103 | |
| 104 | try { |
| 105 | final ServerBootstrap bootstrap = createServerBootStrap(); |
| 106 | |
| 107 | bootstrap.setOption("reuseAddr", true); |
| 108 | bootstrap.setOption("child.keepAlive", true); |
| 109 | bootstrap.setOption("child.tcpNoDelay", true); |
| 110 | bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); |
| 111 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame^] | 112 | ChannelPipelineFactory pfact = new BGPPipelineFactory(bgpController, true); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 113 | |
| 114 | bootstrap.setPipelineFactory(pfact); |
| 115 | InetSocketAddress sa = new InetSocketAddress(getBgpPortNum()); |
| 116 | cg = new DefaultChannelGroup(); |
| 117 | cg.add(bootstrap.bind(sa)); |
| 118 | log.info("Listening for Peer connection on {}", sa); |
| 119 | } catch (Exception e) { |
| 120 | throw new RuntimeException(e); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Creates server boot strap. |
| 126 | * |
| 127 | * @return ServerBootStrap |
| 128 | */ |
| 129 | private ServerBootstrap createServerBootStrap() { |
| 130 | |
| 131 | if (workerThreads == 0) { |
| 132 | serverExecFactory = new NioServerSocketChannelFactory( |
| 133 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")), |
| 134 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d"))); |
| 135 | return new ServerBootstrap(serverExecFactory); |
| 136 | } else { |
| 137 | serverExecFactory = new NioServerSocketChannelFactory( |
| 138 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")), |
| 139 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d")), |
| 140 | workerThreads); |
| 141 | return new ServerBootstrap(serverExecFactory); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Initialize internal data structures. |
| 147 | */ |
| 148 | public void init() { |
| 149 | // These data structures are initialized here because other |
| 150 | // module's startUp() might be called before ours |
| 151 | this.systemStartTime = System.currentTimeMillis(); |
| 152 | } |
| 153 | |
| 154 | // ************** |
| 155 | // Utility methods |
| 156 | // ************** |
| 157 | |
| 158 | public Map<String, Long> getMemory() { |
| 159 | Map<String, Long> m = new HashMap<>(); |
| 160 | Runtime runtime = Runtime.getRuntime(); |
| 161 | m.put("total", runtime.totalMemory()); |
| 162 | m.put("free", runtime.freeMemory()); |
| 163 | return m; |
| 164 | } |
| 165 | |
| 166 | public Long getUptime() { |
| 167 | RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); |
| 168 | return rb.getUptime(); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Starts the BGP controller. |
| 173 | */ |
| 174 | public void start() { |
| 175 | log.info("Started"); |
| 176 | this.init(); |
| 177 | this.run(); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Stops the BGP controller. |
| 182 | */ |
| 183 | public void stop() { |
| 184 | log.info("Stopped"); |
| 185 | serverExecFactory.shutdown(); |
| 186 | cg.close(); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Returns port number. |
| 191 | * |
| 192 | * @return port number |
| 193 | */ |
| 194 | public static short getBgpPortNum() { |
| 195 | return BGP_PORT_NUM; |
| 196 | } |
| 197 | } |