blob: 5eed3cfc4e0727399e8e0fcbe6efd8e358975fa4 [file] [log] [blame]
Satish Ke107e662015-09-21 19:00:17 +05301/*
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 */
16package org.onosproject.bgp.controller.impl;
17
18import static org.onlab.util.Tools.groupedThreads;
19
20import java.lang.management.ManagementFactory;
21import java.lang.management.RuntimeMXBean;
22import java.net.InetSocketAddress;
23import java.util.HashMap;
24import java.util.Map;
25import java.util.concurrent.Executors;
26
27import org.jboss.netty.bootstrap.ServerBootstrap;
28import org.jboss.netty.channel.ChannelPipelineFactory;
29import org.jboss.netty.channel.group.ChannelGroup;
30import org.jboss.netty.channel.group.DefaultChannelGroup;
31import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053032import org.onosproject.bgp.controller.BGPController;
33import org.onosproject.bgpio.protocol.BGPFactories;
34import org.onosproject.bgpio.protocol.BGPFactory;
35import org.onosproject.bgpio.protocol.BGPVersion;
Satish Ke107e662015-09-21 19:00:17 +053036import org.slf4j.Logger;
37import 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 */
43public class Controller {
44
Shashikanth VH9f8afb42015-11-04 18:00:30 +053045 private static final Logger log = LoggerFactory.getLogger(Controller.class);
46
47 private static final BGPFactory FACTORY4 = BGPFactories.getFactory(BGPVersion.BGP_4);
Satish Ke107e662015-09-21 19:00:17 +053048
49 private ChannelGroup cg;
50
51 // Configuration options
52 private static final short BGP_PORT_NUM = 179;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053053 private final int workerThreads = 16;
Satish Ke107e662015-09-21 19:00:17 +053054
55 // Start time of the controller
Shashikanth VH9f8afb42015-11-04 18:00:30 +053056 private long systemStartTime;
Satish Ke107e662015-09-21 19:00:17 +053057
58 private NioServerSocketChannelFactory serverExecFactory;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053059 private BGPController bgpController;
Satish Ke107e662015-09-21 19:00:17 +053060
61 // Perf. related configuration
Shashikanth VH9f8afb42015-11-04 18:00:30 +053062 private static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024;
Satish Ke107e662015-09-21 19:00:17 +053063
64 /**
Shashikanth VH9f8afb42015-11-04 18:00:30 +053065 * Constructor to initialize the values.
Satish Ke107e662015-09-21 19:00:17 +053066 *
Shashikanth VH9f8afb42015-11-04 18:00:30 +053067 * @param bgpController bgp controller instance
Satish Ke107e662015-09-21 19:00:17 +053068 */
Shashikanth VH9f8afb42015-11-04 18:00:30 +053069 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 Ke107e662015-09-21 19:00:17 +053080 }
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 VH9f8afb42015-11-04 18:00:30 +0530112 ChannelPipelineFactory pfact = new BGPPipelineFactory(bgpController, true);
Satish Ke107e662015-09-21 19:00:17 +0530113
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}