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