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 2015-present Open Networking Foundation |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -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 |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -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 | */ |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 16 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 17 | package org.onosproject.openflow.controller.impl; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 18 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 20 | import com.google.common.base.Strings; |
| 21 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Sets; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 23 | import io.netty.bootstrap.ServerBootstrap; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 24 | import io.netty.channel.Channel; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 25 | import io.netty.channel.ChannelOption; |
| 26 | import io.netty.channel.EventLoopGroup; |
| 27 | import io.netty.channel.epoll.EpollEventLoopGroup; |
| 28 | import io.netty.channel.epoll.EpollServerSocketChannel; |
| 29 | import io.netty.channel.group.ChannelGroup; |
| 30 | import io.netty.channel.group.DefaultChannelGroup; |
| 31 | import io.netty.channel.nio.NioEventLoopGroup; |
| 32 | import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 33 | import io.netty.util.concurrent.GlobalEventExecutor; |
Jonathan Hart | a0d1249 | 2015-07-16 12:03:41 -0700 | [diff] [blame] | 34 | import org.onlab.util.ItemNotFoundException; |
| 35 | import org.onosproject.net.DeviceId; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 36 | import org.onosproject.net.config.NetworkConfigRegistry; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 37 | import org.onosproject.net.driver.DefaultDriverData; |
| 38 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 39 | import org.onosproject.net.driver.Driver; |
| 40 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 41 | import org.onosproject.openflow.config.OpenFlowDeviceConfig; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.openflow.controller.Dpid; |
| 43 | import org.onosproject.openflow.controller.driver.OpenFlowAgent; |
| 44 | import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 45 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 46 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 47 | import org.slf4j.Logger; |
| 48 | import org.slf4j.LoggerFactory; |
| 49 | |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 50 | import javax.net.ssl.KeyManagerFactory; |
| 51 | import javax.net.ssl.SSLContext; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 52 | import javax.net.ssl.TrustManagerFactory; |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 53 | import java.io.File; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 54 | import java.io.FileInputStream; |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 55 | import java.io.IOException; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 56 | import java.lang.management.ManagementFactory; |
| 57 | import java.lang.management.RuntimeMXBean; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 58 | import java.net.InetSocketAddress; |
| 59 | import java.net.SocketAddress; |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 60 | import java.security.DigestInputStream; |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 61 | import java.security.KeyManagementException; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 62 | import java.security.KeyStore; |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 63 | import java.security.KeyStoreException; |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 64 | import java.security.MessageDigest; |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 65 | import java.security.NoSuchAlgorithmException; |
| 66 | import java.security.UnrecoverableKeyException; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 67 | import java.security.cert.Certificate; |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 68 | import java.security.cert.CertificateException; |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 69 | import java.util.Arrays; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 70 | import java.util.Collection; |
| 71 | import java.util.Collections; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 72 | import java.util.Dictionary; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 73 | import java.util.EnumSet; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 74 | import java.util.HashMap; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 75 | import java.util.Iterator; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 76 | import java.util.List; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 77 | import java.util.Map; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 78 | import java.util.Objects; |
| 79 | import java.util.Optional; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 80 | import java.util.Set; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 81 | import java.util.stream.Collectors; |
| 82 | import java.util.stream.Stream; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 83 | |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 84 | import static org.onlab.util.Tools.get; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 85 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | 80b0a80 | 2015-07-17 08:43:30 -0700 | [diff] [blame] | 86 | import static org.onosproject.net.DeviceId.deviceId; |
| 87 | import static org.onosproject.openflow.controller.Dpid.uri; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 88 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * The main controller class. Handles all setup and network listeners |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 92 | */ |
| 93 | public class Controller { |
| 94 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 95 | private static final Logger log = LoggerFactory.getLogger(Controller.class); |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 96 | |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 97 | private static final short MIN_KS_LENGTH = 6; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 98 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 99 | protected HashMap<String, String> controllerNodeIPsCache; |
| 100 | |
| 101 | private ChannelGroup cg; |
| 102 | |
| 103 | // Configuration options |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 104 | protected List<Integer> openFlowPorts = ImmutableList.of(6633, 6653); |
Yuta HIGUCHI | 8552b17 | 2016-07-25 12:10:08 -0700 | [diff] [blame] | 105 | protected int workerThreads = 0; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 106 | |
| 107 | // Start time of the controller |
| 108 | protected long systemStartTime; |
| 109 | |
| 110 | private OpenFlowAgent agent; |
| 111 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 112 | private EventLoopGroup bossGroup; |
| 113 | private EventLoopGroup workerGroup; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 114 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 115 | enum TlsMode { |
| 116 | DISABLED, // TLS is not used for OpenFlow connections |
| 117 | ENABLED, // Clients are required use TLS and present a client certificate |
| 118 | STRICT, // Clients must use TLS, and certificate must match the one specified in netcfg |
| 119 | } |
| 120 | private static final EnumSet<TlsMode> TLS_ENABLED = EnumSet.of(TlsMode.ENABLED, TlsMode.STRICT); |
| 121 | |
| 122 | protected TlsParams tlsParams; |
alshabib | 5162a9d | 2015-12-07 17:49:37 -0800 | [diff] [blame] | 123 | protected SSLContext sslContext; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 124 | protected KeyStore keyStore; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 125 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 126 | // Perf. related configuration |
| 127 | protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 128 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 129 | private DriverService driverService; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 130 | private NetworkConfigRegistry netCfgService; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 131 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 132 | |
| 133 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 134 | // ************** |
| 135 | // Initialization |
| 136 | // ************** |
| 137 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 138 | private void addListeningPorts(Collection<Integer> ports) { |
| 139 | if (cg == null) { |
| 140 | return; |
| 141 | } |
| 142 | final ServerBootstrap bootstrap = createServerBootStrap(); |
| 143 | bootstrap.option(ChannelOption.SO_REUSEADDR, true); |
| 144 | bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); |
| 145 | bootstrap.childOption(ChannelOption.TCP_NODELAY, true); |
| 146 | bootstrap.childOption(ChannelOption.SO_SNDBUF, Controller.SEND_BUFFER_SIZE); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 147 | // bootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, |
| 148 | // new WriteBufferWaterMark(8 * 1024, 32 * 1024)); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 149 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 150 | bootstrap.childHandler(new OFChannelInitializer(this, null, sslContext)); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 151 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 152 | Set<Integer> existingPorts = cg.stream() |
| 153 | .map(Channel::localAddress) |
| 154 | .filter(InetSocketAddress.class::isInstance) |
| 155 | .map(InetSocketAddress.class::cast) |
| 156 | .map(InetSocketAddress::getPort) |
| 157 | .collect(Collectors.toSet()); |
| 158 | ports.removeAll(existingPorts); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 159 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 160 | ports.forEach(port -> { |
| 161 | // TODO revisit if this is best way to listen to multiple ports |
| 162 | cg.add(bootstrap.bind(port).syncUninterruptibly().channel()); |
| 163 | log.info("Listening for OF switch connections on {}", port); |
| 164 | }); |
| 165 | } |
| 166 | |
| 167 | private void removeListeningPorts(Collection<Integer> ports) { |
| 168 | if (cg == null) { |
| 169 | return; |
| 170 | } |
| 171 | Iterator<Channel> itr = cg.iterator(); |
| 172 | while (itr.hasNext()) { |
| 173 | Channel c = itr.next(); |
| 174 | SocketAddress addr = c.localAddress(); |
| 175 | if (addr instanceof InetSocketAddress) { |
| 176 | InetSocketAddress inetAddr = (InetSocketAddress) addr; |
| 177 | Integer port = inetAddr.getPort(); |
| 178 | if (ports.contains(port)) { |
| 179 | log.info("No longer listening for OF switch connections on {}", port); |
| 180 | c.close(); |
| 181 | itr.remove(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | private ServerBootstrap createServerBootStrap() { |
| 189 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 190 | int bossThreads = Math.max(1, openFlowPorts.size()); |
| 191 | try { |
| 192 | bossGroup = new EpollEventLoopGroup(bossThreads, groupedThreads("onos/of", "boss-%d", log)); |
| 193 | workerGroup = new EpollEventLoopGroup(workerThreads, groupedThreads("onos/of", "worker-%d", log)); |
| 194 | ServerBootstrap bs = new ServerBootstrap() |
| 195 | .group(bossGroup, workerGroup) |
| 196 | .channel(EpollServerSocketChannel.class); |
| 197 | log.info("Using Epoll transport"); |
| 198 | return bs; |
| 199 | } catch (Throwable e) { |
| 200 | log.debug("Failed to initialize native (epoll) transport: {}", e.getMessage()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 201 | } |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 202 | |
| 203 | // Requires 4.1.11 or later |
| 204 | // try { |
| 205 | // bossGroup = new KQueueEventLoopGroup(bossThreads, groupedThreads("onos/of", "boss-%d", log)); |
| 206 | // workerGroup = new KQueueEventLoopGroup(workerThreads, groupedThreads("onos/of", "worker-%d", log)); |
| 207 | // ServerBootstrap bs = new ServerBootstrap() |
| 208 | // .group(bossGroup, workerGroup) |
| 209 | // .channel(KQueueServerSocketChannel.class); |
| 210 | // log.info("Using Kqueue transport"); |
| 211 | // return bs; |
| 212 | // } catch (Throwable e) { |
| 213 | // log.debug("Failed to initialize native (kqueue) transport. ", e.getMessage()); |
| 214 | // } |
| 215 | |
| 216 | bossGroup = new NioEventLoopGroup(bossThreads, groupedThreads("onos/of", "boss-%d", log)); |
| 217 | workerGroup = new NioEventLoopGroup(workerThreads, groupedThreads("onos/of", "worker-%d", log)); |
| 218 | log.info("Using Nio transport"); |
| 219 | return new ServerBootstrap() |
| 220 | .group(bossGroup, workerGroup) |
| 221 | .channel(NioServerSocketChannel.class); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 224 | public void setConfigParams(Dictionary<?, ?> properties) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 225 | boolean restartRequired = setOpenFlowPorts(properties); |
| 226 | restartRequired |= setWorkerThreads(properties); |
| 227 | restartRequired |= setTlsParameters(properties); |
| 228 | if (restartRequired) { |
| 229 | restart(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 230 | } |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Gets the list of listening ports from property dict. |
| 235 | * |
| 236 | * @param properties dictionary |
| 237 | * @return true if restart is required |
| 238 | */ |
| 239 | private boolean setWorkerThreads(Dictionary<?, ?> properties) { |
| 240 | List<Integer> oldPorts = this.openFlowPorts; |
| 241 | String ports = get(properties, "openflowPorts"); |
| 242 | List<Integer> newPorts = Collections.emptyList(); |
| 243 | if (!Strings.isNullOrEmpty(ports)) { |
| 244 | newPorts = Stream.of(ports.split(",")) |
| 245 | .map(s -> Integer.parseInt(s)) |
| 246 | .collect(Collectors.toList()); |
| 247 | } |
| 248 | |
| 249 | Set<Integer> portsToAdd = Sets.newHashSet(newPorts); |
| 250 | portsToAdd.removeAll(oldPorts); |
| 251 | addListeningPorts(portsToAdd); |
| 252 | |
| 253 | Set<Integer> portsToRemove = Sets.newHashSet(oldPorts); |
| 254 | portsToRemove.removeAll(newPorts); |
| 255 | removeListeningPorts(portsToRemove); |
| 256 | |
| 257 | this.openFlowPorts = newPorts; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 258 | log.debug("OpenFlow ports set to {}", this.openFlowPorts); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 259 | return false; // restart is never required |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Gets the number of worker threads from property dict. |
| 264 | * |
| 265 | * @param properties dictionary |
| 266 | * @return true if restart is required |
| 267 | */ |
| 268 | private boolean setOpenFlowPorts(Dictionary<?, ?> properties) { |
| 269 | Integer oldValue = this.workerThreads; |
Charles Chan | 45624b8 | 2015-08-24 00:29:20 +0800 | [diff] [blame] | 270 | |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 271 | String threads = get(properties, "workerThreads"); |
| 272 | if (!Strings.isNullOrEmpty(threads)) { |
| 273 | this.workerThreads = Integer.parseInt(threads); |
| 274 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 275 | log.debug("Number of worker threads set to {}", this.workerThreads); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 276 | return oldValue != this.workerThreads; // restart if number of threads has changed |
| 277 | } |
| 278 | |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 279 | static class TlsParams { |
| 280 | final TlsMode mode; |
| 281 | final String ksLocation; |
| 282 | final String tsLocation; |
| 283 | final String ksPwd; |
| 284 | final String tsPwd; |
| 285 | final byte[] ksSignature; |
| 286 | final byte[] tsSignature; |
| 287 | |
| 288 | TlsParams(TlsMode mode, String ksLocation, String tsLocation, |
| 289 | String ksPwd, String tsPwd) { |
| 290 | this.mode = mode; |
| 291 | this.ksLocation = ksLocation; |
| 292 | this.tsLocation = tsLocation; |
| 293 | this.ksPwd = ksPwd; |
| 294 | this.tsPwd = tsPwd; |
| 295 | this.ksSignature = getSha1Checksum(ksLocation); |
| 296 | this.tsSignature = getSha1Checksum(tsLocation); |
| 297 | } |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 298 | |
| 299 | public char[] ksPwd() { |
| 300 | return ksPwd.toCharArray(); |
| 301 | } |
| 302 | |
| 303 | public char[] tsPwd() { |
| 304 | return tsPwd.toCharArray(); |
| 305 | } |
| 306 | |
| 307 | public boolean isTlsEnabled() { |
| 308 | return TLS_ENABLED.contains(mode); |
| 309 | } |
| 310 | |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 311 | public byte[] getSha1Checksum(String filepath) { |
| 312 | if (filepath == null) { |
| 313 | return new byte[0]; |
| 314 | } |
| 315 | try { |
| 316 | MessageDigest digest = MessageDigest.getInstance("SHA1"); |
| 317 | File f = new File(filepath); |
| 318 | FileInputStream is = new FileInputStream(f); |
| 319 | DigestInputStream dis = new DigestInputStream(is, digest); |
| 320 | byte[] buffer = new byte[1024]; |
| 321 | while (dis.read(buffer) > 0) { |
| 322 | // nothing to do :) |
| 323 | } |
| 324 | dis.getMessageDigest().digest(); |
| 325 | } catch (NoSuchAlgorithmException ignored) { |
| 326 | } catch (IOException e) { |
| 327 | log.info("Error reading file file: {}", filepath); |
| 328 | } |
| 329 | return new byte[0]; |
| 330 | } |
| 331 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 332 | @Override |
| 333 | public int hashCode() { |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 334 | if (mode == TlsMode.DISABLED) { |
| 335 | return Objects.hash(mode); |
| 336 | } |
| 337 | return Objects.hash(mode, ksLocation, tsLocation, |
Yuta HIGUCHI | cf03a0f | 2018-05-15 19:25:28 -0700 | [diff] [blame^] | 338 | ksPwd, tsPwd, |
| 339 | Arrays.hashCode(ksSignature), |
| 340 | Arrays.hashCode(tsSignature)); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public boolean equals(Object obj) { |
| 345 | if (this == obj) { |
| 346 | return true; |
| 347 | } |
| 348 | if (obj instanceof TlsParams) { |
| 349 | final TlsParams that = (TlsParams) obj; |
| 350 | if (this.getClass() != that.getClass()) { |
| 351 | return false; |
| 352 | } else if (this.mode == that.mode && this.mode == TlsMode.DISABLED) { |
| 353 | // All disabled objects should be equal regardless of other params |
| 354 | return true; |
| 355 | } |
| 356 | return this.mode == that.mode && |
| 357 | Objects.equals(this.ksLocation, that.ksLocation) && |
| 358 | Objects.equals(this.tsLocation, that.tsLocation) && |
| 359 | Objects.equals(this.ksPwd, that.ksPwd) && |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 360 | Objects.equals(this.tsPwd, that.tsPwd) && |
| 361 | Arrays.equals(this.ksSignature, that.ksSignature) && |
| 362 | Arrays.equals(this.tsSignature, that.tsSignature); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 363 | } |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | @Override |
| 368 | public String toString() { |
| 369 | return MoreObjects.toStringHelper(this) |
| 370 | .add("tlsMode", mode.toString().toLowerCase()) |
| 371 | .add("ksLocation", ksLocation) |
| 372 | .add("tsLocation", tsLocation) |
| 373 | .toString(); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Gets the TLS parameters from the properties dict, but fallback to the old approach of |
| 379 | * system properties if the parameters are missing from the dict. |
| 380 | * |
| 381 | * @param properties dictionary |
| 382 | * @return true if restart is required |
| 383 | */ |
| 384 | private boolean setTlsParameters(Dictionary<?, ?> properties) { |
| 385 | TlsParams oldParams = this.tlsParams; |
| 386 | |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 387 | TlsMode mode = null; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 388 | String tlsString = get(properties, "tlsMode"); |
| 389 | if (!Strings.isNullOrEmpty(tlsString)) { |
| 390 | try { |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 391 | mode = TlsMode.valueOf(tlsString.toUpperCase()); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 392 | } catch (IllegalArgumentException e) { |
| 393 | log.info("Invalid TLS mode {}. TLS is disabled.", tlsString); |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 394 | mode = TlsMode.DISABLED; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 395 | } |
| 396 | } else { |
| 397 | // Fallback to system properties |
| 398 | // TODO this method of configuring TLS is deprecated and should be removed eventually |
| 399 | tlsString = System.getProperty("enableOFTLS"); |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 400 | mode = !Strings.isNullOrEmpty(tlsString) && Boolean.parseBoolean(tlsString) ? |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 401 | TlsMode.ENABLED : TlsMode.DISABLED; |
| 402 | } |
| 403 | |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 404 | String ksLocation = null, tsLocation = null, ksPwd = null, tsPwd = null; |
| 405 | if (TLS_ENABLED.contains(mode)) { |
| 406 | ksLocation = get(properties, "keyStore"); |
| 407 | if (Strings.isNullOrEmpty(ksLocation)) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 408 | // Fallback to system properties |
| 409 | // TODO remove this eventually |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 410 | ksLocation = System.getProperty("javax.net.ssl.keyStore"); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 411 | } |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 412 | if (Strings.isNullOrEmpty(ksLocation)) { |
| 413 | mode = TlsMode.DISABLED; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 416 | tsLocation = get(properties, "trustStore"); |
| 417 | if (Strings.isNullOrEmpty(tsLocation)) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 418 | // Fallback to system properties |
| 419 | // TODO remove this eventually |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 420 | tsLocation = System.getProperty("javax.net.ssl.trustStore"); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 421 | } |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 422 | if (Strings.isNullOrEmpty(tsLocation)) { |
| 423 | mode = TlsMode.DISABLED; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 426 | ksPwd = get(properties, "keyStorePassword"); |
| 427 | if (Strings.isNullOrEmpty(ksPwd)) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 428 | // Fallback to system properties |
| 429 | // TODO remove this eventually |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 430 | ksPwd = System.getProperty("javax.net.ssl.keyStorePassword"); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 431 | } |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 432 | if (Strings.isNullOrEmpty(ksPwd) || MIN_KS_LENGTH > ksPwd.length()) { |
| 433 | mode = TlsMode.DISABLED; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 436 | tsPwd = get(properties, "trustStorePassword"); |
| 437 | if (Strings.isNullOrEmpty(tsPwd)) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 438 | // Fallback to system properties |
| 439 | // TODO remove this eventually |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 440 | tsPwd = System.getProperty("javax.net.ssl.trustStorePassword"); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 441 | } |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 442 | if (Strings.isNullOrEmpty(tsPwd) || MIN_KS_LENGTH > tsPwd.length()) { |
| 443 | mode = TlsMode.DISABLED; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 446 | this.tlsParams = new TlsParams(mode, ksLocation, tsLocation, ksPwd, tsPwd); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 447 | log.info("OpenFlow TLS Params: {}", tlsParams); |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 448 | return !Objects.equals(this.tlsParams, oldParams); // restart if TLS params change |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 449 | } |
| 450 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 451 | /** |
| 452 | * Initialize internal data structures. |
| 453 | */ |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 454 | public void init() { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 455 | // These data structures are initialized here because other |
| 456 | // module's startUp() might be called before ours |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 457 | this.controllerNodeIPsCache = new HashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 458 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 459 | this.systemStartTime = System.currentTimeMillis(); |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 460 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 461 | cg = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); |
| 462 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 463 | if (tlsParams.isTlsEnabled()) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 464 | initSsl(); |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 465 | } |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 468 | private void initSsl() { |
| 469 | try { |
| 470 | TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 471 | KeyStore ts = KeyStore.getInstance("JKS"); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 472 | ts.load(new FileInputStream(tlsParams.tsLocation), tlsParams.tsPwd()); |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 473 | tmFactory.init(ts); |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 474 | |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 475 | KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 476 | keyStore = KeyStore.getInstance("JKS"); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 477 | keyStore.load(new FileInputStream(tlsParams.ksLocation), tlsParams.ksPwd()); |
| 478 | kmf.init(keyStore, tlsParams.ksPwd()); |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 479 | |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 480 | sslContext = SSLContext.getInstance("TLS"); |
| 481 | sslContext.init(kmf.getKeyManagers(), tmFactory.getTrustManagers(), null); |
| 482 | } catch (NoSuchAlgorithmException | KeyStoreException | CertificateException | |
| 483 | IOException | KeyManagementException | UnrecoverableKeyException ex) { |
| 484 | log.error("SSL init failed: {}", ex.getMessage()); |
| 485 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | // ************** |
| 489 | // Utility methods |
| 490 | // ************** |
| 491 | |
| 492 | public Map<String, Long> getMemory() { |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 493 | Map<String, Long> m = new HashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 494 | Runtime runtime = Runtime.getRuntime(); |
| 495 | m.put("total", runtime.totalMemory()); |
| 496 | m.put("free", runtime.freeMemory()); |
| 497 | return m; |
| 498 | } |
| 499 | |
| 500 | |
Ray Milkey | dc0ff19 | 2015-11-04 13:49:52 -0800 | [diff] [blame] | 501 | public Long getSystemUptime() { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 502 | RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); |
| 503 | return rb.getUptime(); |
| 504 | } |
| 505 | |
Ray Milkey | dc0ff19 | 2015-11-04 13:49:52 -0800 | [diff] [blame] | 506 | public long getSystemStartTime() { |
| 507 | return (this.systemStartTime); |
| 508 | } |
| 509 | |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 510 | public boolean isValidCertificate(Long dpid, Certificate peerCert) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 511 | if (!tlsParams.isTlsEnabled()) { |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 512 | return true; |
| 513 | } |
| 514 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 515 | if (netCfgService == null) { |
| 516 | // netcfg service not available; accept any cert if not in strict mode |
| 517 | return tlsParams.mode == TlsMode.ENABLED; |
| 518 | } |
| 519 | |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 520 | DeviceId deviceId = DeviceId.deviceId(Dpid.uri(new Dpid(dpid))); |
| 521 | OpenFlowDeviceConfig config = |
| 522 | netCfgService.getConfig(deviceId, OpenFlowDeviceConfig.class); |
| 523 | if (config == null) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 524 | // Config not set for device, accept any cert if not in strict mode |
| 525 | return tlsParams.mode == TlsMode.ENABLED; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | Optional<String> alias = config.keyAlias(); |
| 529 | if (!alias.isPresent()) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 530 | // Config for device does not specify a certificate chain, accept any cert if not in strict mode |
| 531 | return tlsParams.mode == TlsMode.ENABLED; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | try { |
| 535 | Certificate configuredCert = keyStore.getCertificate(alias.get()); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 536 | //TODO there's probably a better way to compare these |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 537 | return Objects.deepEquals(peerCert, configuredCert); |
| 538 | } catch (KeyStoreException e) { |
| 539 | log.info("failed to load key", e); |
| 540 | } |
| 541 | return false; |
| 542 | } |
| 543 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 544 | /** |
| 545 | * Forward to the driver-manager to get an IOFSwitch instance. |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 546 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 547 | * @param dpid data path id |
| 548 | * @param desc switch description |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 549 | * @param ofv OpenFlow version |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 550 | * @return switch instance |
| 551 | */ |
| 552 | protected OpenFlowSwitchDriver getOFSwitchInstance(long dpid, |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 553 | OFDescStatsReply desc, |
| 554 | OFVersion ofv) { |
Jonathan Hart | a0d1249 | 2015-07-16 12:03:41 -0700 | [diff] [blame] | 555 | Dpid dpidObj = new Dpid(dpid); |
| 556 | |
| 557 | Driver driver; |
| 558 | try { |
Sho SHIMIZU | bc82ebb | 2015-08-25 10:15:21 -0700 | [diff] [blame] | 559 | driver = driverService.getDriver(DeviceId.deviceId(Dpid.uri(dpidObj))); |
Jonathan Hart | a0d1249 | 2015-07-16 12:03:41 -0700 | [diff] [blame] | 560 | } catch (ItemNotFoundException e) { |
| 561 | driver = driverService.getDriver(desc.getMfrDesc(), desc.getHwDesc(), desc.getSwDesc()); |
| 562 | } |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 563 | |
Jonathan Hart | a33134e | 2016-07-27 17:33:35 -0700 | [diff] [blame] | 564 | if (driver == null) { |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 565 | log.error("No OpenFlow driver for {} : {}", dpidObj, desc); |
Jonathan Hart | a33134e | 2016-07-27 17:33:35 -0700 | [diff] [blame] | 566 | return null; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 567 | } |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 568 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 569 | log.info("Driver '{}' assigned to device {}", driver.name(), dpidObj); |
Jonathan Hart | a33134e | 2016-07-27 17:33:35 -0700 | [diff] [blame] | 570 | |
| 571 | if (!driver.hasBehaviour(OpenFlowSwitchDriver.class)) { |
| 572 | log.error("Driver {} does not support OpenFlowSwitchDriver behaviour", driver.name()); |
| 573 | return null; |
| 574 | } |
| 575 | |
| 576 | DefaultDriverHandler handler = |
| 577 | new DefaultDriverHandler(new DefaultDriverData(driver, deviceId(uri(dpidObj)))); |
| 578 | OpenFlowSwitchDriver ofSwitchDriver = |
| 579 | driver.createBehaviour(handler, OpenFlowSwitchDriver.class); |
| 580 | ofSwitchDriver.init(dpidObj, desc, ofv); |
| 581 | ofSwitchDriver.setAgent(agent); |
| 582 | ofSwitchDriver.setRoleHandler(new RoleManager(ofSwitchDriver)); |
Jonathan Hart | a33134e | 2016-07-27 17:33:35 -0700 | [diff] [blame] | 583 | return ofSwitchDriver; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 586 | @Deprecated |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 587 | public void start(OpenFlowAgent ag, DriverService driverService) { |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 588 | start(ag, driverService, null); |
| 589 | } |
| 590 | |
| 591 | public void start(OpenFlowAgent ag, DriverService driverService, |
| 592 | NetworkConfigRegistry netCfgService) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 593 | log.info("Starting OpenFlow IO"); |
| 594 | this.agent = ag; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 595 | this.driverService = driverService; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 596 | this.netCfgService = netCfgService; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 597 | this.init(); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 598 | this.addListeningPorts(this.openFlowPorts); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | |
| 602 | public void stop() { |
| 603 | log.info("Stopping OpenFlow IO"); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 604 | if (cg != null) { |
| 605 | cg.close(); |
| 606 | cg = null; |
| 607 | } |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 608 | |
| 609 | // Shut down all event loops to terminate all threads. |
| 610 | bossGroup.shutdownGracefully(); |
| 611 | workerGroup.shutdownGracefully(); |
| 612 | |
| 613 | // Wait until all threads are terminated. |
| 614 | try { |
| 615 | bossGroup.terminationFuture().sync(); |
| 616 | workerGroup.terminationFuture().sync(); |
| 617 | } catch (InterruptedException e) { |
| 618 | log.warn("Interrupted while stopping", e); |
| 619 | Thread.currentThread().interrupt(); |
| 620 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 623 | private void restart() { |
| 624 | // only restart if we are already running |
| 625 | if (cg != null) { |
| 626 | stop(); |
| 627 | start(this.agent, this.driverService, this.netCfgService); |
| 628 | } |
| 629 | } |
| 630 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 631 | } |