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