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