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