Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame^] | 16 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 17 | package org.onosproject.routing.bgp; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 18 | |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Component; |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Modified; |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 26 | import org.jboss.netty.bootstrap.ServerBootstrap; |
| 27 | import org.jboss.netty.channel.Channel; |
| 28 | import org.jboss.netty.channel.ChannelException; |
| 29 | import org.jboss.netty.channel.ChannelFactory; |
| 30 | import org.jboss.netty.channel.ChannelPipeline; |
| 31 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 32 | import org.jboss.netty.channel.Channels; |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 33 | import org.jboss.netty.channel.group.ChannelGroup; |
| 34 | import org.jboss.netty.channel.group.DefaultChannelGroup; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 35 | import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 36 | import org.onlab.packet.Ip4Address; |
| 37 | import org.onlab.packet.Ip4Prefix; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 38 | import org.onlab.packet.Ip6Prefix; |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 39 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 40 | import org.onosproject.incubator.net.routing.Route; |
| 41 | import org.onosproject.incubator.net.routing.RouteAdminService; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 42 | import org.osgi.service.component.ComponentContext; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 43 | import org.slf4j.Logger; |
| 44 | import org.slf4j.LoggerFactory; |
| 45 | |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 46 | import java.net.InetAddress; |
| 47 | import java.net.InetSocketAddress; |
| 48 | import java.net.SocketAddress; |
| 49 | import java.util.Collection; |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 50 | import java.util.Dictionary; |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 51 | import java.util.concurrent.ConcurrentHashMap; |
| 52 | import java.util.concurrent.ConcurrentMap; |
| 53 | |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 54 | import static java.util.concurrent.Executors.newCachedThreadPool; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 55 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 56 | |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 57 | /** |
| 58 | * BGP Session Manager class. |
| 59 | */ |
Jonathan Hart | c22e847 | 2015-11-17 18:25:45 -0800 | [diff] [blame] | 60 | @Component(immediate = true, enabled = false) |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 61 | @Service |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 62 | public class BgpSessionManager implements BgpInfoService { |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 63 | private static final Logger log = |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 64 | LoggerFactory.getLogger(BgpSessionManager.class); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 65 | |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected RouteAdminService routeService; |
| 68 | |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 69 | boolean isShutdown = true; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 70 | private Channel serverChannel; // Listener for incoming BGP connections |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 71 | private ServerBootstrap serverBootstrap; |
| 72 | private ChannelGroup allChannels = new DefaultChannelGroup(); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 73 | private ConcurrentMap<SocketAddress, BgpSession> bgpSessions = |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 74 | new ConcurrentHashMap<>(); |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 75 | private Ip4Address myBgpId; // Same BGP ID for all peers |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 76 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 77 | private BgpRouteSelector bgpRouteSelector = new BgpRouteSelector(this); |
| 78 | private ConcurrentMap<Ip4Prefix, BgpRouteEntry> bgpRoutes4 = |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 79 | new ConcurrentHashMap<>(); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 80 | private ConcurrentMap<Ip6Prefix, BgpRouteEntry> bgpRoutes6 = |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 81 | new ConcurrentHashMap<>(); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 82 | |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 83 | private static final int DEFAULT_BGP_PORT = 2000; |
| 84 | private int bgpPort; |
| 85 | |
| 86 | @Activate |
| 87 | protected void activate(ComponentContext context) { |
| 88 | readComponentConfiguration(context); |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 89 | start(); |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 90 | log.info("BgpSessionManager started"); |
| 91 | } |
| 92 | |
| 93 | @Deactivate |
| 94 | protected void deactivate() { |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 95 | stop(); |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 96 | log.info("BgpSessionManager stopped"); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Extracts properties from the component configuration context. |
| 101 | * |
| 102 | * @param context the component context |
| 103 | */ |
| 104 | private void readComponentConfiguration(ComponentContext context) { |
| 105 | Dictionary<?, ?> properties = context.getProperties(); |
| 106 | try { |
| 107 | String strPort = (String) properties.get("bgpPort"); |
| 108 | if (strPort != null) { |
| 109 | bgpPort = Integer.parseInt(strPort); |
| 110 | } else { |
| 111 | bgpPort = DEFAULT_BGP_PORT; |
| 112 | } |
Pavlin Radoslavov | b9e50df | 2015-02-20 20:01:26 -0800 | [diff] [blame] | 113 | } catch (NumberFormatException | ClassCastException e) { |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 114 | bgpPort = DEFAULT_BGP_PORT; |
| 115 | } |
| 116 | log.debug("BGP port is set to {}", bgpPort); |
| 117 | } |
| 118 | |
| 119 | @Modified |
| 120 | public void modified(ComponentContext context) { |
| 121 | // Blank @Modified method to catch modifications to the context. |
| 122 | // If no @Modified method exists, it seems @Activate is called again |
| 123 | // when the context is modified. |
| 124 | } |
| 125 | |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 126 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 127 | * Checks whether the BGP Session Manager is shutdown. |
| 128 | * |
| 129 | * @return true if the BGP Session Manager is shutdown, otherwise false |
| 130 | */ |
| 131 | boolean isShutdown() { |
| 132 | return this.isShutdown; |
| 133 | } |
| 134 | |
| 135 | /** |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 136 | * Gets the BGP sessions. |
| 137 | * |
| 138 | * @return the BGP sessions |
| 139 | */ |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 140 | @Override |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 141 | public Collection<BgpSession> getBgpSessions() { |
| 142 | return bgpSessions.values(); |
| 143 | } |
| 144 | |
| 145 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 146 | * Gets the selected IPv4 BGP routes among all BGP sessions. |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 147 | * |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 148 | * @return the selected IPv4 BGP routes among all BGP sessions |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 149 | */ |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 150 | @Override |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 151 | public Collection<BgpRouteEntry> getBgpRoutes4() { |
| 152 | return bgpRoutes4.values(); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Gets the selected IPv6 BGP routes among all BGP sessions. |
| 157 | * |
| 158 | * @return the selected IPv6 BGP routes among all BGP sessions |
| 159 | */ |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 160 | @Override |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 161 | public Collection<BgpRouteEntry> getBgpRoutes6() { |
| 162 | return bgpRoutes6.values(); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Finds a BGP route for a prefix. The prefix can be either IPv4 or IPv6. |
| 167 | * |
| 168 | * @param prefix the prefix to use |
| 169 | * @return the BGP route if found, otherwise null |
| 170 | */ |
| 171 | BgpRouteEntry findBgpRoute(IpPrefix prefix) { |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 172 | if (prefix.isIp4()) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 173 | return bgpRoutes4.get(prefix.getIp4Prefix()); // IPv4 |
| 174 | } |
| 175 | return bgpRoutes6.get(prefix.getIp6Prefix()); // IPv6 |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Adds a BGP route. The route can be either IPv4 or IPv6. |
| 180 | * |
| 181 | * @param bgpRouteEntry the BGP route entry to use |
| 182 | */ |
| 183 | void addBgpRoute(BgpRouteEntry bgpRouteEntry) { |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 184 | if (bgpRouteEntry.isIp4()) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 185 | bgpRoutes4.put(bgpRouteEntry.prefix().getIp4Prefix(), // IPv4 |
| 186 | bgpRouteEntry); |
| 187 | } else { |
| 188 | bgpRoutes6.put(bgpRouteEntry.prefix().getIp6Prefix(), // IPv6 |
| 189 | bgpRouteEntry); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Removes a BGP route for a prefix. The prefix can be either IPv4 or IPv6. |
| 195 | * |
| 196 | * @param prefix the prefix to use |
| 197 | * @return true if the route was found and removed, otherwise false |
| 198 | */ |
| 199 | boolean removeBgpRoute(IpPrefix prefix) { |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 200 | if (prefix.isIp4()) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 201 | return (bgpRoutes4.remove(prefix.getIp4Prefix()) != null); // IPv4 |
| 202 | } |
| 203 | return (bgpRoutes6.remove(prefix.getIp6Prefix()) != null); // IPv6 |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 207 | * Adds the channel for a BGP session. |
| 208 | * |
| 209 | * @param channel the channel to add |
| 210 | */ |
| 211 | void addSessionChannel(Channel channel) { |
| 212 | allChannels.add(channel); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Removes the channel for a BGP session. |
| 217 | * |
| 218 | * @param channel the channel to remove |
| 219 | */ |
| 220 | void removeSessionChannel(Channel channel) { |
| 221 | allChannels.remove(channel); |
| 222 | } |
| 223 | |
| 224 | /** |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 225 | * Processes the connection from a BGP peer. |
| 226 | * |
| 227 | * @param bgpSession the BGP session for the peer |
| 228 | * @return true if the connection can be established, otherwise false |
| 229 | */ |
| 230 | boolean peerConnected(BgpSession bgpSession) { |
| 231 | |
| 232 | // Test whether there is already a session from the same remote |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 233 | if (bgpSessions.get(bgpSession.remoteInfo().address()) != null) { |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 234 | return false; // Duplicate BGP session |
| 235 | } |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 236 | bgpSessions.put(bgpSession.remoteInfo().address(), bgpSession); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 237 | |
| 238 | // |
| 239 | // If the first connection, set my BGP ID to the local address |
| 240 | // of the socket. |
| 241 | // |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 242 | if (bgpSession.localInfo().address() instanceof InetSocketAddress) { |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 243 | InetAddress inetAddr = |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 244 | ((InetSocketAddress) bgpSession.localInfo().address()).getAddress(); |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 245 | Ip4Address ip4Address = Ip4Address.valueOf(inetAddr.getAddress()); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 246 | updateMyBgpId(ip4Address); |
| 247 | } |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Processes the disconnection from a BGP peer. |
| 253 | * |
| 254 | * @param bgpSession the BGP session for the peer |
| 255 | */ |
| 256 | void peerDisconnected(BgpSession bgpSession) { |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 257 | bgpSessions.remove(bgpSession.remoteInfo().address()); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Conditionally updates the local BGP ID if it wasn't set already. |
| 262 | * <p/> |
| 263 | * NOTE: A BGP instance should use same BGP ID across all BGP sessions. |
| 264 | * |
| 265 | * @param ip4Address the IPv4 address to use as BGP ID |
| 266 | */ |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 267 | private synchronized void updateMyBgpId(Ip4Address ip4Address) { |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 268 | if (myBgpId == null) { |
| 269 | myBgpId = ip4Address; |
| 270 | log.debug("BGP: My BGP ID is {}", myBgpId); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Gets the local BGP Identifier as an IPv4 address. |
| 276 | * |
| 277 | * @return the local BGP Identifier as an IPv4 address |
| 278 | */ |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 279 | Ip4Address getMyBgpId() { |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 280 | return myBgpId; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Gets the BGP Route Selector. |
| 285 | * |
| 286 | * @return the BGP Route Selector |
| 287 | */ |
| 288 | BgpRouteSelector getBgpRouteSelector() { |
| 289 | return bgpRouteSelector; |
| 290 | } |
| 291 | |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 292 | /** |
| 293 | * Sends updates routes to the route service. |
| 294 | * |
| 295 | * @param updates routes to update |
| 296 | */ |
| 297 | void update(Collection<Route> updates) { |
| 298 | routeService.update(updates); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Sends withdrawn routes to the routes service. |
| 303 | * |
| 304 | * @param withdraws routes to withdraw |
| 305 | */ |
| 306 | void withdraw(Collection<Route> withdraws) { |
| 307 | routeService.withdraw(withdraws); |
| 308 | } |
| 309 | |
| 310 | |
| 311 | public void start() { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 312 | log.debug("BGP Session Manager start."); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 313 | isShutdown = false; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 314 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 315 | ChannelFactory channelFactory = new NioServerSocketChannelFactory( |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 316 | newCachedThreadPool(groupedThreads("onos/bgp", "sm-boss-%d", log)), |
| 317 | newCachedThreadPool(groupedThreads("onos/bgp", "sm-worker-%d", log))); |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 318 | ChannelPipelineFactory pipelineFactory = () -> { |
| 319 | // Allocate a new session per connection |
| 320 | BgpSession bgpSessionHandler = |
| 321 | new BgpSession(BgpSessionManager.this); |
| 322 | BgpFrameDecoder bgpFrameDecoder = |
| 323 | new BgpFrameDecoder(bgpSessionHandler); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 324 | |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 325 | // Setup the processing pipeline |
| 326 | ChannelPipeline pipeline = Channels.pipeline(); |
| 327 | pipeline.addLast("BgpFrameDecoder", bgpFrameDecoder); |
| 328 | pipeline.addLast("BgpSession", bgpSessionHandler); |
| 329 | return pipeline; |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 330 | }; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 331 | InetSocketAddress listenAddress = |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 332 | new InetSocketAddress(bgpPort); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 333 | |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 334 | serverBootstrap = new ServerBootstrap(channelFactory); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 335 | // serverBootstrap.setOptions("reuseAddr", true); |
| 336 | serverBootstrap.setOption("child.keepAlive", true); |
| 337 | serverBootstrap.setOption("child.tcpNoDelay", true); |
| 338 | serverBootstrap.setPipelineFactory(pipelineFactory); |
| 339 | try { |
| 340 | serverChannel = serverBootstrap.bind(listenAddress); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 341 | allChannels.add(serverChannel); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 342 | } catch (ChannelException e) { |
| 343 | log.debug("Exception binding to BGP port {}: ", |
| 344 | listenAddress.getPort(), e); |
| 345 | } |
| 346 | } |
| 347 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 348 | public void stop() { |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 349 | isShutdown = true; |
| 350 | allChannels.close().awaitUninterruptibly(); |
| 351 | serverBootstrap.releaseExternalResources(); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 352 | } |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 353 | } |