senthil | da68c65 | 2024-03-22 19:29:28 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2024-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.bgpmonitoring.impl; |
| 18 | |
| 19 | |
| 20 | import org.jboss.netty.bootstrap.ServerBootstrap; |
| 21 | import org.jboss.netty.channel.AdaptiveReceiveBufferSizePredictor; |
| 22 | import org.jboss.netty.channel.Channel; |
| 23 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 24 | import org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory; |
| 25 | import org.jboss.netty.channel.group.ChannelGroup; |
| 26 | import org.jboss.netty.channel.group.DefaultChannelGroup; |
| 27 | import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; |
| 28 | import org.onosproject.bgpmonitoring.BmpController; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.lang.management.ManagementFactory; |
| 33 | import java.lang.management.RuntimeMXBean; |
| 34 | import java.net.InetSocketAddress; |
| 35 | import java.util.HashMap; |
| 36 | import java.util.Map; |
| 37 | import java.util.concurrent.Executors; |
| 38 | |
| 39 | import static org.onlab.util.Tools.groupedThreads; |
| 40 | |
| 41 | /** |
| 42 | * The main controller class. Handles all setup and network listeners - Ownership of bmp message receiver |
| 43 | */ |
| 44 | public class Controller { |
| 45 | |
| 46 | private static final Logger log = LoggerFactory.getLogger(Controller.class); |
| 47 | |
| 48 | private ChannelGroup channelGroup; |
| 49 | private Channel serverChannel; |
| 50 | |
| 51 | protected static final short BMP_PORT_NUM = 9000; |
| 52 | private final int workerThreads = 16; |
| 53 | |
| 54 | private long systemStartTime; |
| 55 | |
| 56 | private NioServerSocketChannelFactory serverExecFactory; |
| 57 | private BmpController bmpController; |
| 58 | |
| 59 | private static final int SEND_BUFFER_SIZE = 8 * 1024 * 1024; |
| 60 | |
| 61 | /** |
| 62 | * Constructor to initialize the values. |
| 63 | * |
| 64 | * @param bmpController bmp controller instance |
| 65 | */ |
| 66 | public Controller(BmpController bmpController) { |
| 67 | this.bmpController = bmpController; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * To get system start time. |
| 72 | * |
| 73 | * @return system start time in milliseconds |
| 74 | */ |
| 75 | public long getSystemStartTime() { |
| 76 | return (this.systemStartTime); |
| 77 | } |
| 78 | |
| 79 | public void init() { |
| 80 | this.systemStartTime = System.currentTimeMillis(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Gets run time memory. |
| 85 | * |
| 86 | * @return m run time memory |
| 87 | */ |
| 88 | public Map<String, Long> getMemory() { |
| 89 | Map<String, Long> m = new HashMap<>(); |
| 90 | Runtime runtime = Runtime.getRuntime(); |
| 91 | m.put("total", runtime.totalMemory()); |
| 92 | m.put("free", runtime.freeMemory()); |
| 93 | return m; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Gets UP time. |
| 98 | * |
| 99 | * @return UP time |
| 100 | */ |
| 101 | public Long getUptime() { |
| 102 | RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); |
| 103 | return rb.getUptime(); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Tell controller it will accept bmp router connections. |
| 108 | */ |
| 109 | public void run() { |
| 110 | |
| 111 | try { |
| 112 | |
| 113 | final ServerBootstrap bootstrap = createServerBootStrap(); |
| 114 | |
| 115 | bootstrap.setOption("reuseAddress", true); |
| 116 | bootstrap.setOption("tcpNoDelay", true); |
| 117 | bootstrap.setOption("keepAlive", true); |
| 118 | bootstrap.setOption("receiveBufferSize", Controller.SEND_BUFFER_SIZE); |
| 119 | bootstrap.setOption("receiveBufferSizePredictorFactory", |
| 120 | new FixedReceiveBufferSizePredictorFactory( |
| 121 | Controller.SEND_BUFFER_SIZE)); |
| 122 | bootstrap.setOption("receiveBufferSizePredictor", |
| 123 | new AdaptiveReceiveBufferSizePredictor(64, 4096, 65536)); |
| 124 | bootstrap.setOption("child.keepAlive", true); |
| 125 | bootstrap.setOption("child.tcpNoDelay", true); |
| 126 | bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); |
| 127 | bootstrap.setOption("child.receiveBufferSize", Controller.SEND_BUFFER_SIZE); |
| 128 | bootstrap.setOption("child.receiveBufferSizePredictorFactory", |
| 129 | new FixedReceiveBufferSizePredictorFactory( |
| 130 | Controller.SEND_BUFFER_SIZE)); |
| 131 | bootstrap.setOption("child.reuseAddress", true); |
| 132 | |
senthil | 389bca6 | 2024-03-22 20:46:30 +0530 | [diff] [blame] | 133 | ChannelPipelineFactory pfact = new BmpPipelineFactory(bmpController); |
senthil | da68c65 | 2024-03-22 19:29:28 +0530 | [diff] [blame] | 134 | |
| 135 | bootstrap.setPipelineFactory(pfact); |
| 136 | InetSocketAddress inetSocketAddress = new InetSocketAddress(BMP_PORT_NUM); |
| 137 | channelGroup = new DefaultChannelGroup(); |
| 138 | serverChannel = bootstrap.bind(inetSocketAddress); |
| 139 | channelGroup.add(serverChannel); |
| 140 | log.info("Listening for bmp router connection on {}", inetSocketAddress); |
| 141 | } catch (Exception e) { |
| 142 | throw new IllegalStateException(e); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Creates server boot strap. |
| 148 | * |
| 149 | * @return ServerBootStrap |
| 150 | */ |
| 151 | private ServerBootstrap createServerBootStrap() { |
| 152 | |
| 153 | if (workerThreads == 0) { |
| 154 | serverExecFactory = new NioServerSocketChannelFactory( |
| 155 | Executors.newCachedThreadPool(groupedThreads("onos/bmp", "boss-%d")), |
| 156 | Executors.newCachedThreadPool(groupedThreads("onos/bmp", "worker-%d"))); |
| 157 | return new ServerBootstrap(serverExecFactory); |
| 158 | } else { |
| 159 | serverExecFactory = new NioServerSocketChannelFactory( |
| 160 | Executors.newCachedThreadPool(groupedThreads("onos/bmp", "boss-%d")), |
| 161 | Executors.newCachedThreadPool(groupedThreads("onos/bmp", "worker-%d")), |
| 162 | workerThreads); |
| 163 | return new ServerBootstrap(serverExecFactory); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Stops the BMP controller. |
| 169 | */ |
| 170 | public void stop() { |
| 171 | log.info("Stopped"); |
| 172 | serverExecFactory.shutdown(); |
| 173 | channelGroup.close(); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Starts the BMP controller. |
| 178 | */ |
| 179 | public void start() { |
| 180 | log.info("Started"); |
| 181 | this.run(); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | } |