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 |
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 | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 19 | import com.google.common.base.Strings; |
| 20 | import com.google.common.collect.ImmutableList; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 21 | |
| 22 | import io.netty.bootstrap.ServerBootstrap; |
| 23 | import io.netty.channel.ChannelOption; |
| 24 | import io.netty.channel.EventLoopGroup; |
| 25 | import io.netty.channel.epoll.EpollEventLoopGroup; |
| 26 | import io.netty.channel.epoll.EpollServerSocketChannel; |
| 27 | import io.netty.channel.group.ChannelGroup; |
| 28 | import io.netty.channel.group.DefaultChannelGroup; |
| 29 | import io.netty.channel.nio.NioEventLoopGroup; |
| 30 | import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 31 | import io.netty.util.concurrent.GlobalEventExecutor; |
alshabib | 5162a9d | 2015-12-07 17:49:37 -0800 | [diff] [blame] | 32 | |
Jonathan Hart | a0d1249 | 2015-07-16 12:03:41 -0700 | [diff] [blame] | 33 | import org.onlab.util.ItemNotFoundException; |
| 34 | import org.onosproject.net.DeviceId; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 35 | import org.onosproject.net.driver.DefaultDriverData; |
| 36 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 37 | import org.onosproject.net.driver.Driver; |
| 38 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.openflow.controller.Dpid; |
| 40 | import org.onosproject.openflow.controller.driver.OpenFlowAgent; |
| 41 | import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 42 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
| 43 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 44 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 45 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 49 | import javax.net.ssl.KeyManagerFactory; |
| 50 | import javax.net.ssl.SSLContext; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 51 | import javax.net.ssl.TrustManagerFactory; |
| 52 | import java.io.FileInputStream; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 53 | import java.lang.management.ManagementFactory; |
| 54 | import java.lang.management.RuntimeMXBean; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 55 | import java.security.KeyStore; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 56 | import java.util.Dictionary; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 57 | import java.util.HashMap; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 58 | import java.util.List; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 59 | import java.util.Map; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 60 | import java.util.stream.Collectors; |
| 61 | import java.util.stream.Stream; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 62 | |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 63 | import static org.onlab.util.Tools.get; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 64 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | 80b0a80 | 2015-07-17 08:43:30 -0700 | [diff] [blame] | 65 | import static org.onosproject.net.DeviceId.deviceId; |
| 66 | import static org.onosproject.openflow.controller.Dpid.uri; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 67 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * The main controller class. Handles all setup and network listeners |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 71 | */ |
| 72 | public class Controller { |
| 73 | |
| 74 | protected static final Logger log = LoggerFactory.getLogger(Controller.class); |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 75 | |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 76 | /** |
| 77 | * @deprecated in 1.10.0 |
| 78 | */ |
| 79 | @Deprecated |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 80 | protected static final OFFactory FACTORY13 = OFFactories.getFactory(OFVersion.OF_13); |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 81 | /** |
| 82 | * @deprecated in 1.10.0 |
| 83 | */ |
| 84 | @Deprecated |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 85 | protected static final OFFactory FACTORY10 = OFFactories.getFactory(OFVersion.OF_10); |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 86 | |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 87 | private static final boolean TLS_DISABLED = false; |
| 88 | private static final short MIN_KS_LENGTH = 6; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 89 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 90 | protected HashMap<String, String> controllerNodeIPsCache; |
| 91 | |
| 92 | private ChannelGroup cg; |
| 93 | |
| 94 | // Configuration options |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 95 | protected List<Integer> openFlowPorts = ImmutableList.of(6633, 6653); |
Yuta HIGUCHI | 8552b17 | 2016-07-25 12:10:08 -0700 | [diff] [blame] | 96 | protected int workerThreads = 0; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 97 | |
| 98 | // Start time of the controller |
| 99 | protected long systemStartTime; |
| 100 | |
| 101 | private OpenFlowAgent agent; |
| 102 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 103 | private EventLoopGroup bossGroup; |
| 104 | private EventLoopGroup workerGroup; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 105 | |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 106 | protected String ksLocation; |
| 107 | protected String tsLocation; |
| 108 | protected char[] ksPwd; |
| 109 | protected char[] tsPwd; |
alshabib | 5162a9d | 2015-12-07 17:49:37 -0800 | [diff] [blame] | 110 | protected SSLContext sslContext; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 111 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 112 | // Perf. related configuration |
| 113 | protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 114 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 115 | private DriverService driverService; |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 116 | private boolean enableOfTls = TLS_DISABLED; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 117 | |
| 118 | // *************** |
| 119 | // Getters/Setters |
| 120 | // *************** |
| 121 | |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 122 | /** |
| 123 | * @return OF1.0 factory |
| 124 | * @deprecated in 1.10.0 |
| 125 | */ |
| 126 | @Deprecated |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 127 | public OFFactory getOFMessageFactory10() { |
| 128 | return FACTORY10; |
| 129 | } |
| 130 | |
| 131 | |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 132 | /** |
| 133 | * @return OF1.3 factory |
| 134 | * @deprecated in 1.10.0 |
| 135 | */ |
| 136 | @Deprecated |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 137 | public OFFactory getOFMessageFactory13() { |
| 138 | return FACTORY13; |
| 139 | } |
| 140 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 141 | // ************** |
| 142 | // Initialization |
| 143 | // ************** |
| 144 | |
| 145 | /** |
| 146 | * Tell controller that we're ready to accept switches loop. |
| 147 | */ |
| 148 | public void run() { |
| 149 | |
| 150 | try { |
| 151 | final ServerBootstrap bootstrap = createServerBootStrap(); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 152 | bootstrap.option(ChannelOption.SO_REUSEADDR, true); |
| 153 | bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); |
| 154 | bootstrap.childOption(ChannelOption.TCP_NODELAY, true); |
| 155 | bootstrap.childOption(ChannelOption.SO_SNDBUF, Controller.SEND_BUFFER_SIZE); |
| 156 | // bootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, |
| 157 | // new WriteBufferWaterMark(8 * 1024, 32 * 1024)); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 158 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 159 | bootstrap.childHandler(new OFChannelInitializer(this, null, sslContext)); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 160 | |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 161 | openFlowPorts.forEach(port -> { |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 162 | // TODO revisit if this is best way to listen to multiple ports |
| 163 | cg.add(bootstrap.bind(port).syncUninterruptibly().channel()); |
| 164 | log.info("Listening for switch connections on {}", port); |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 165 | }); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 166 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 167 | } catch (Exception e) { |
| 168 | throw new RuntimeException(e); |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | private ServerBootstrap createServerBootStrap() { |
| 174 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 175 | int bossThreads = Math.max(1, openFlowPorts.size()); |
| 176 | try { |
| 177 | bossGroup = new EpollEventLoopGroup(bossThreads, groupedThreads("onos/of", "boss-%d", log)); |
| 178 | workerGroup = new EpollEventLoopGroup(workerThreads, groupedThreads("onos/of", "worker-%d", log)); |
| 179 | ServerBootstrap bs = new ServerBootstrap() |
| 180 | .group(bossGroup, workerGroup) |
| 181 | .channel(EpollServerSocketChannel.class); |
| 182 | log.info("Using Epoll transport"); |
| 183 | return bs; |
| 184 | } catch (Throwable e) { |
| 185 | log.debug("Failed to initialize native (epoll) transport: {}", e.getMessage()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 186 | } |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 187 | |
| 188 | // Requires 4.1.11 or later |
| 189 | // try { |
| 190 | // bossGroup = new KQueueEventLoopGroup(bossThreads, groupedThreads("onos/of", "boss-%d", log)); |
| 191 | // workerGroup = new KQueueEventLoopGroup(workerThreads, groupedThreads("onos/of", "worker-%d", log)); |
| 192 | // ServerBootstrap bs = new ServerBootstrap() |
| 193 | // .group(bossGroup, workerGroup) |
| 194 | // .channel(KQueueServerSocketChannel.class); |
| 195 | // log.info("Using Kqueue transport"); |
| 196 | // return bs; |
| 197 | // } catch (Throwable e) { |
| 198 | // log.debug("Failed to initialize native (kqueue) transport. ", e.getMessage()); |
| 199 | // } |
| 200 | |
| 201 | bossGroup = new NioEventLoopGroup(bossThreads, groupedThreads("onos/of", "boss-%d", log)); |
| 202 | workerGroup = new NioEventLoopGroup(workerThreads, groupedThreads("onos/of", "worker-%d", log)); |
| 203 | log.info("Using Nio transport"); |
| 204 | return new ServerBootstrap() |
| 205 | .group(bossGroup, workerGroup) |
| 206 | .channel(NioServerSocketChannel.class); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 209 | public void setConfigParams(Dictionary<?, ?> properties) { |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 210 | // TODO should be possible to reconfigure ports without restart, |
| 211 | // by updating ChannelGroup |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 212 | String ports = get(properties, "openflowPorts"); |
| 213 | if (!Strings.isNullOrEmpty(ports)) { |
| 214 | this.openFlowPorts = Stream.of(ports.split(",")) |
| 215 | .map(s -> Integer.parseInt(s)) |
| 216 | .collect(Collectors.toList()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 217 | } |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 218 | log.debug("OpenFlow ports set to {}", this.openFlowPorts); |
Charles Chan | 45624b8 | 2015-08-24 00:29:20 +0800 | [diff] [blame] | 219 | |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 220 | String threads = get(properties, "workerThreads"); |
| 221 | if (!Strings.isNullOrEmpty(threads)) { |
| 222 | this.workerThreads = Integer.parseInt(threads); |
| 223 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 224 | log.debug("Number of worker threads set to {}", this.workerThreads); |
| 225 | } |
| 226 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 227 | /** |
| 228 | * Initialize internal data structures. |
| 229 | */ |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 230 | public void init() { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 231 | // These data structures are initialized here because other |
| 232 | // module's startUp() might be called before ours |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 233 | this.controllerNodeIPsCache = new HashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 234 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 235 | this.systemStartTime = System.currentTimeMillis(); |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 236 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 237 | cg = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); |
| 238 | |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 239 | try { |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 240 | getTlsParameters(); |
| 241 | if (enableOfTls) { |
| 242 | initSsl(); |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 243 | } |
| 244 | } catch (Exception ex) { |
| 245 | log.error("SSL init failed: {}", ex.getMessage()); |
| 246 | } |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 249 | private void getTlsParameters() { |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 250 | String tempString = System.getProperty("enableOFTLS"); |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 251 | enableOfTls = Strings.isNullOrEmpty(tempString) ? TLS_DISABLED : Boolean.parseBoolean(tempString); |
| 252 | log.info("OpenFlow Security is {}", enableOfTls ? "enabled" : "disabled"); |
| 253 | if (enableOfTls) { |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 254 | ksLocation = System.getProperty("javax.net.ssl.keyStore"); |
| 255 | if (Strings.isNullOrEmpty(ksLocation)) { |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 256 | enableOfTls = TLS_DISABLED; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 257 | return; |
| 258 | } |
| 259 | tsLocation = System.getProperty("javax.net.ssl.trustStore"); |
| 260 | if (Strings.isNullOrEmpty(tsLocation)) { |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 261 | enableOfTls = TLS_DISABLED; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 262 | return; |
| 263 | } |
| 264 | ksPwd = System.getProperty("javax.net.ssl.keyStorePassword").toCharArray(); |
| 265 | if (MIN_KS_LENGTH > ksPwd.length) { |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 266 | enableOfTls = TLS_DISABLED; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | tsPwd = System.getProperty("javax.net.ssl.trustStorePassword").toCharArray(); |
| 270 | if (MIN_KS_LENGTH > tsPwd.length) { |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 271 | enableOfTls = TLS_DISABLED; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 272 | return; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 277 | private void initSsl() throws Exception { |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 278 | TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 279 | KeyStore ts = KeyStore.getInstance("JKS"); |
| 280 | ts.load(new FileInputStream(tsLocation), tsPwd); |
| 281 | tmFactory.init(ts); |
| 282 | |
| 283 | KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
| 284 | KeyStore ks = KeyStore.getInstance("JKS"); |
| 285 | ks.load(new FileInputStream(ksLocation), ksPwd); |
| 286 | kmf.init(ks, ksPwd); |
| 287 | |
alshabib | 5162a9d | 2015-12-07 17:49:37 -0800 | [diff] [blame] | 288 | sslContext = SSLContext.getInstance("TLS"); |
| 289 | sslContext.init(kmf.getKeyManagers(), tmFactory.getTrustManagers(), null); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // ************** |
| 293 | // Utility methods |
| 294 | // ************** |
| 295 | |
| 296 | public Map<String, Long> getMemory() { |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 297 | Map<String, Long> m = new HashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 298 | Runtime runtime = Runtime.getRuntime(); |
| 299 | m.put("total", runtime.totalMemory()); |
| 300 | m.put("free", runtime.freeMemory()); |
| 301 | return m; |
| 302 | } |
| 303 | |
| 304 | |
Ray Milkey | dc0ff19 | 2015-11-04 13:49:52 -0800 | [diff] [blame] | 305 | public Long getSystemUptime() { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 306 | RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); |
| 307 | return rb.getUptime(); |
| 308 | } |
| 309 | |
Ray Milkey | dc0ff19 | 2015-11-04 13:49:52 -0800 | [diff] [blame] | 310 | public long getSystemStartTime() { |
| 311 | return (this.systemStartTime); |
| 312 | } |
| 313 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 314 | /** |
| 315 | * Forward to the driver-manager to get an IOFSwitch instance. |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 316 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 317 | * @param dpid data path id |
| 318 | * @param desc switch description |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 319 | * @param ofv OpenFlow version |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 320 | * @return switch instance |
| 321 | */ |
| 322 | protected OpenFlowSwitchDriver getOFSwitchInstance(long dpid, |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 323 | OFDescStatsReply desc, |
| 324 | OFVersion ofv) { |
Jonathan Hart | a0d1249 | 2015-07-16 12:03:41 -0700 | [diff] [blame] | 325 | Dpid dpidObj = new Dpid(dpid); |
| 326 | |
| 327 | Driver driver; |
| 328 | try { |
Sho SHIMIZU | bc82ebb | 2015-08-25 10:15:21 -0700 | [diff] [blame] | 329 | driver = driverService.getDriver(DeviceId.deviceId(Dpid.uri(dpidObj))); |
Jonathan Hart | a0d1249 | 2015-07-16 12:03:41 -0700 | [diff] [blame] | 330 | } catch (ItemNotFoundException e) { |
| 331 | driver = driverService.getDriver(desc.getMfrDesc(), desc.getHwDesc(), desc.getSwDesc()); |
| 332 | } |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 333 | |
Jonathan Hart | a33134e | 2016-07-27 17:33:35 -0700 | [diff] [blame] | 334 | if (driver == null) { |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 335 | log.error("No OpenFlow driver for {} : {}", dpidObj, desc); |
Jonathan Hart | a33134e | 2016-07-27 17:33:35 -0700 | [diff] [blame] | 336 | return null; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 337 | } |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 338 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 339 | log.info("Driver '{}' assigned to device {}", driver.name(), dpidObj); |
Jonathan Hart | a33134e | 2016-07-27 17:33:35 -0700 | [diff] [blame] | 340 | |
| 341 | if (!driver.hasBehaviour(OpenFlowSwitchDriver.class)) { |
| 342 | log.error("Driver {} does not support OpenFlowSwitchDriver behaviour", driver.name()); |
| 343 | return null; |
| 344 | } |
| 345 | |
| 346 | DefaultDriverHandler handler = |
| 347 | new DefaultDriverHandler(new DefaultDriverData(driver, deviceId(uri(dpidObj)))); |
| 348 | OpenFlowSwitchDriver ofSwitchDriver = |
| 349 | driver.createBehaviour(handler, OpenFlowSwitchDriver.class); |
| 350 | ofSwitchDriver.init(dpidObj, desc, ofv); |
| 351 | ofSwitchDriver.setAgent(agent); |
| 352 | ofSwitchDriver.setRoleHandler(new RoleManager(ofSwitchDriver)); |
Jonathan Hart | a33134e | 2016-07-27 17:33:35 -0700 | [diff] [blame] | 353 | return ofSwitchDriver; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 354 | } |
| 355 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 356 | public void start(OpenFlowAgent ag, DriverService driverService) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 357 | log.info("Starting OpenFlow IO"); |
| 358 | this.agent = ag; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 359 | this.driverService = driverService; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 360 | this.init(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 361 | this.run(); |
| 362 | } |
| 363 | |
| 364 | |
| 365 | public void stop() { |
| 366 | log.info("Stopping OpenFlow IO"); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 367 | cg.close(); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 368 | |
| 369 | // Shut down all event loops to terminate all threads. |
| 370 | bossGroup.shutdownGracefully(); |
| 371 | workerGroup.shutdownGracefully(); |
| 372 | |
| 373 | // Wait until all threads are terminated. |
| 374 | try { |
| 375 | bossGroup.terminationFuture().sync(); |
| 376 | workerGroup.terminationFuture().sync(); |
| 377 | } catch (InterruptedException e) { |
| 378 | log.warn("Interrupted while stopping", e); |
| 379 | Thread.currentThread().interrupt(); |
| 380 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | } |