Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 1 | package net.onrc.onos.registry.controller; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 2 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 3 | import java.io.IOException; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 4 | import java.io.UnsupportedEncodingException; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 5 | import java.util.ArrayList; |
| 6 | import java.util.Collection; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 7 | import java.util.Collections; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 8 | import java.util.HashMap; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 9 | import java.util.List; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 10 | import java.util.Map; |
| 11 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 12 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 13 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 14 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 15 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 16 | import net.floodlightcontroller.restserver.IRestApiService; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 17 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 18 | import org.apache.zookeeper.CreateMode; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 19 | import org.apache.zookeeper.WatchedEvent; |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 20 | import org.apache.zookeeper.Watcher.Event.KeeperState; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 21 | import org.openflow.util.HexString; |
| 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 24 | |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 25 | import com.google.common.base.Charsets; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 26 | import com.netflix.curator.RetryPolicy; |
| 27 | import com.netflix.curator.framework.CuratorFramework; |
| 28 | import com.netflix.curator.framework.CuratorFrameworkFactory; |
| 29 | import com.netflix.curator.framework.api.CuratorWatcher; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 30 | import com.netflix.curator.framework.recipes.cache.ChildData; |
| 31 | import com.netflix.curator.framework.recipes.cache.PathChildrenCache; |
| 32 | import com.netflix.curator.framework.recipes.cache.PathChildrenCache.StartMode; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 33 | import com.netflix.curator.framework.recipes.cache.PathChildrenCacheEvent; |
| 34 | import com.netflix.curator.framework.recipes.cache.PathChildrenCacheListener; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 35 | import com.netflix.curator.framework.recipes.leader.LeaderLatch; |
| 36 | import com.netflix.curator.framework.recipes.leader.Participant; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 37 | import com.netflix.curator.retry.ExponentialBackoffRetry; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 38 | |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 39 | /** |
| 40 | * A registry service that uses Zookeeper. All data is stored in Zookeeper, |
| 41 | * so this can be used as a global registry in a multi-node ONOS cluster. |
| 42 | * @author jono |
| 43 | * |
| 44 | */ |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 45 | public class ZookeeperRegistry implements IFloodlightModule, IControllerRegistryService { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 46 | |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 47 | protected static Logger log = LoggerFactory.getLogger(ZookeeperRegistry.class); |
| 48 | protected String controllerId = null; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 49 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 50 | protected IRestApiService restApi; |
| 51 | |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 52 | //This is the default, it's overwritten by the connectionString configuration parameter |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 53 | protected String connectionString = "localhost:2181"; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 54 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 55 | private final String namespace = "onos"; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 56 | private final String switchLatchesPath = "/switches"; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 57 | private final String controllerPath = "/controllers"; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 58 | |
| 59 | protected CuratorFramework client; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 60 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 61 | protected PathChildrenCache controllerCache; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 62 | protected PathChildrenCache switchCache; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 63 | |
| 64 | protected Map<String, LeaderLatch> switchLatches; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 65 | protected Map<String, ControlChangeCallback> switchCallbacks; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 66 | protected Map<String, PathChildrenCache> switchPathCaches; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 67 | |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 68 | //Zookeeper performance-related configuration |
Jonathan Hart | cc957a0 | 2013-02-26 10:39:04 -0800 | [diff] [blame] | 69 | protected static final int sessionTimeout = 2000; |
| 70 | protected static final int connectionTimeout = 4000; |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 71 | |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 72 | /** |
| 73 | * Watches for changes in switch leadership election. The Curator |
| 74 | * LeaderLatch doesn't notify us when leadership changes so we set a watch |
| 75 | * on the election znodes to get leadership change events. The process |
| 76 | * method will be called whenever the switches children change in |
| 77 | * Zookeeper. We then have to work out whether to send a control-changed |
| 78 | * event to our clients and reset the watch. |
| 79 | * |
| 80 | * TODO I think it's possible to miss events that happen while we're |
| 81 | * processing the watch and before we've set a new watch. Need to think |
| 82 | * of a safer way to implement leader change notifications. |
| 83 | * |
| 84 | */ |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 85 | protected class ParamaterizedCuratorWatcher implements CuratorWatcher { |
| 86 | private String dpid; |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 87 | private boolean isLeader = false; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 88 | private String latchPath; |
| 89 | |
| 90 | public ParamaterizedCuratorWatcher(String dpid, String latchPath){ |
| 91 | this.dpid = dpid; |
| 92 | this.latchPath = latchPath; |
| 93 | } |
| 94 | |
| 95 | @Override |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 96 | public synchronized void process(WatchedEvent event) throws Exception { |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 97 | log.debug("Watch Event: {}", event); |
| 98 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 99 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 100 | if (event.getState() == KeeperState.Disconnected){ |
| 101 | if (isLeader) { |
| 102 | log.debug("Disconnected while leader - lost leadership for {}", dpid); |
| 103 | |
| 104 | isLeader = false; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 105 | ControlChangeCallback cb = switchCallbacks.get(dpid); |
| 106 | if (cb != null) { |
| 107 | //Allow callback to be null if the requester doesn't want a callback |
| 108 | cb.controlChanged(HexString.toLong(dpid), false); |
| 109 | } |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 110 | } |
| 111 | return; |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 112 | //TODO Watcher is never reset once we reconnect to Zookeeper |
| 113 | } |
| 114 | |
| 115 | LeaderLatch latch = switchLatches.get(dpid); |
| 116 | if (latch == null){ |
| 117 | log.debug("In watcher process, looks like control was released for {}", |
| 118 | dpid); |
| 119 | return; |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 120 | } |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 121 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 122 | try { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 123 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 124 | Participant leader = latch.getLeader(); |
| 125 | |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 126 | if (leader.getId().equals(controllerId) && !isLeader){ |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 127 | log.debug("Became leader for {}", dpid); |
| 128 | |
| 129 | isLeader = true; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 130 | switchCallbacks.get(dpid).controlChanged(HexString.toLong(dpid), true); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 131 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 132 | else if (!leader.getId().equals(controllerId) && isLeader){ |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 133 | log.debug("Lost leadership for {}", dpid); |
| 134 | |
| 135 | isLeader = false; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 136 | switchCallbacks.get(dpid).controlChanged(HexString.toLong(dpid), false); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 137 | } |
| 138 | } catch (Exception e){ |
| 139 | if (isLeader){ |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 140 | log.debug("Exception checking leadership status. Assume leadership lost for {}", |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 141 | dpid); |
| 142 | |
| 143 | isLeader = false; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 144 | switchCallbacks.get(dpid).controlChanged(HexString.toLong(dpid), false); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 145 | } |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 146 | } finally { |
| 147 | client.getChildren().usingWatcher(this).inBackground().forPath(latchPath); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 148 | } |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 149 | //client.getChildren().usingWatcher(this).forPath(latchPath); |
| 150 | } |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 151 | } |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 152 | |
| 153 | |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 154 | /** |
| 155 | * Listens for changes to the switch znodes in Zookeeper. This maintains |
| 156 | * the second level of PathChildrenCaches that hold the controllers |
| 157 | * contending for each switch - there's one for each switch. |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 158 | */ |
| 159 | PathChildrenCacheListener switchPathCacheListener = new PathChildrenCacheListener() { |
| 160 | @Override |
| 161 | public void childEvent(CuratorFramework client, |
| 162 | PathChildrenCacheEvent event) throws Exception { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 163 | log.debug("Root switch path cache got {} event", event.getType()); |
| 164 | |
| 165 | String strSwitch = null; |
| 166 | if (event.getData() != null){ |
| 167 | log.debug("Event path {}", event.getData().getPath()); |
| 168 | String[] splitted = event.getData().getPath().split("/"); |
| 169 | strSwitch = splitted[splitted.length - 1]; |
| 170 | log.debug("Switch name is {}", strSwitch); |
| 171 | } |
| 172 | |
| 173 | switch (event.getType()){ |
| 174 | case CHILD_ADDED: |
| 175 | case CHILD_UPDATED: |
| 176 | //Check we have a PathChildrenCache for this child, add one if not |
| 177 | if (switchPathCaches.get(strSwitch) == null){ |
| 178 | PathChildrenCache pc = new PathChildrenCache(client, |
| 179 | event.getData().getPath(), true); |
| 180 | pc.start(StartMode.NORMAL); |
| 181 | switchPathCaches.put(strSwitch, pc); |
| 182 | } |
| 183 | break; |
| 184 | case CHILD_REMOVED: |
| 185 | //Remove our PathChildrenCache for this child |
| 186 | PathChildrenCache pc = switchPathCaches.remove(strSwitch); |
| 187 | pc.close(); |
| 188 | break; |
| 189 | default: |
| 190 | //All other events are connection status events. We need to do anything |
| 191 | //as the path cache handles these on its own. |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | } |
| 196 | }; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 197 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 198 | |
| 199 | @Override |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 200 | public void requestControl(long dpid, ControlChangeCallback cb) throws RegistryException { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 201 | log.info("Requesting control for {}", HexString.toHexString(dpid)); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 202 | |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 203 | if (controllerId == null){ |
| 204 | throw new RuntimeException("Must register a controller before calling requestControl"); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | String dpidStr = HexString.toHexString(dpid); |
| 208 | String latchPath = switchLatchesPath + "/" + dpidStr; |
| 209 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 210 | if (switchLatches.get(dpidStr) != null){ |
Jonathan Hart | 3c0eccd | 2013-03-12 22:32:50 -0700 | [diff] [blame^] | 211 | //throw new RuntimeException("Leader election for switch " + dpidStr + |
| 212 | // "is already running"); |
| 213 | log.debug("Already contesting {}, returning", HexString.toHexString(dpid)); |
| 214 | return; |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 217 | LeaderLatch latch = new LeaderLatch(client, latchPath, controllerId); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 218 | switchLatches.put(dpidStr, latch); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 219 | switchCallbacks.put(dpidStr, cb); |
| 220 | |
| 221 | try { |
| 222 | //client.getChildren().usingWatcher(watcher).inBackground().forPath(singleLatchPath); |
| 223 | client.getChildren().usingWatcher( |
| 224 | new ParamaterizedCuratorWatcher(dpidStr, latchPath)) |
| 225 | .inBackground().forPath(latchPath); |
| 226 | latch.start(); |
| 227 | } catch (Exception e) { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 228 | log.warn("Error starting leader latch: {}", e.getMessage()); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 229 | throw new RegistryException("Error starting leader latch for " + dpidStr, e); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | } |
| 233 | |
| 234 | @Override |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 235 | public void releaseControl(long dpid) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 236 | log.info("Releasing control for {}", HexString.toHexString(dpid)); |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 237 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 238 | String dpidStr = HexString.toHexString(dpid); |
| 239 | |
| 240 | LeaderLatch latch = switchLatches.get(dpidStr); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 241 | if (latch == null) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 242 | log.debug("Trying to release control of a switch we are not contesting"); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | |
| 246 | try { |
| 247 | latch.close(); |
| 248 | } catch (IOException e) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 249 | //I think it's OK not to do anything here. Either the node got |
| 250 | //deleted correctly, or the connection went down and the node got deleted. |
Jonathan Hart | 1be4626 | 2013-02-20 16:43:51 -0800 | [diff] [blame] | 251 | } finally { |
| 252 | switchLatches.remove(dpidStr); |
| 253 | switchCallbacks.remove(dpidStr); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | @Override |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 258 | public boolean hasControl(long dpid) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 259 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 260 | LeaderLatch latch = switchLatches.get(HexString.toHexString(dpid)); |
| 261 | |
| 262 | if (latch == null) { |
| 263 | log.warn("No leader latch for dpid {}", HexString.toHexString(dpid)); |
| 264 | return false; |
| 265 | } |
| 266 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 267 | try { |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 268 | return latch.getLeader().getId().equals(controllerId); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 269 | } catch (Exception e) { |
| 270 | //TODO swallow exception? |
| 271 | return false; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | @Override |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 276 | public String getControllerId() { |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 277 | return controllerId; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 280 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 281 | public Collection<String> getAllControllers() throws RegistryException { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 282 | log.debug("Getting all controllers"); |
Jonathan Hart | 1be4626 | 2013-02-20 16:43:51 -0800 | [diff] [blame] | 283 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 284 | List<String> controllers = new ArrayList<String>(); |
| 285 | for (ChildData data : controllerCache.getCurrentData()){ |
| 286 | |
| 287 | String d = null; |
| 288 | try { |
| 289 | d = new String(data.getData(), "UTF-8"); |
| 290 | } catch (UnsupportedEncodingException e) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 291 | throw new RegistryException("Error encoding string", e); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | controllers.add(d); |
| 295 | } |
| 296 | return controllers; |
| 297 | } |
| 298 | |
| 299 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 300 | public void registerController(String id) throws RegistryException { |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 301 | if (controllerId != null) { |
| 302 | throw new RegistryException( |
| 303 | "Controller already registered with id " + controllerId); |
| 304 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 305 | |
| 306 | controllerId = id; |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 307 | |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 308 | byte bytes[] = id.getBytes(Charsets.UTF_8); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 309 | |
| 310 | String path = controllerPath + "/" + id; |
| 311 | |
| 312 | log.info("Registering controller with id {}", id); |
| 313 | |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 314 | //Create ephemeral node in controller registry |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 315 | try { |
| 316 | client.create().withProtection().withMode(CreateMode.EPHEMERAL) |
| 317 | .forPath(path, bytes); |
| 318 | } catch (Exception e) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 319 | throw new RegistryException("Error contacting the Zookeeper service", e); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 324 | public String getControllerForSwitch(long dpid) throws RegistryException { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 325 | // TODO Work out how we should store this controller/switch data. |
| 326 | // The leader latch might be a index to the /controllers collections |
| 327 | // which holds more info on the controller (how to talk to it for example). |
| 328 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 329 | String strDpid = HexString.toHexString(dpid); |
| 330 | LeaderLatch latch = switchLatches.get(strDpid); |
| 331 | |
| 332 | if (latch == null){ |
| 333 | log.warn("Tried to get controller for non-existent switch"); |
| 334 | return null; |
| 335 | } |
| 336 | |
| 337 | Participant leader = null; |
| 338 | try { |
| 339 | leader = latch.getLeader(); |
| 340 | } catch (Exception e) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 341 | throw new RegistryException("Error contacting the Zookeeper service", e); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | return leader.getId(); |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public Collection<Long> getSwitchesControlledByController(String controllerId) { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 349 | //TODO remove this if not needed |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 350 | throw new RuntimeException("Not yet implemented"); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 351 | } |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 352 | |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 353 | |
| 354 | @Override |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 355 | public Map<String, List<ControllerRegistryEntry>> getAllSwitches() { |
| 356 | Map<String, List<ControllerRegistryEntry>> data = |
| 357 | new HashMap<String, List<ControllerRegistryEntry>>(); |
| 358 | |
| 359 | for (Map.Entry<String, PathChildrenCache> entry : switchPathCaches.entrySet()){ |
| 360 | List<ControllerRegistryEntry> contendingControllers = |
| 361 | new ArrayList<ControllerRegistryEntry>(); |
| 362 | |
| 363 | if (entry.getValue().getCurrentData().size() < 1){ |
| 364 | log.info("Switch entry with no leader elections: {}", entry.getKey()); |
| 365 | continue; |
| 366 | } |
| 367 | |
| 368 | for (ChildData d : entry.getValue().getCurrentData()) { |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 369 | |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 370 | String controllerId = new String(d.getData(), Charsets.UTF_8); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 371 | |
| 372 | String[] splitted = d.getPath().split("-"); |
| 373 | int sequenceNumber = Integer.parseInt(splitted[splitted.length - 1]); |
| 374 | |
| 375 | contendingControllers.add(new ControllerRegistryEntry(controllerId, sequenceNumber)); |
| 376 | } |
| 377 | |
| 378 | Collections.sort(contendingControllers); |
| 379 | data.put(entry.getKey(), contendingControllers); |
| 380 | } |
| 381 | return data; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 384 | /* |
| 385 | * IFloodlightModule |
| 386 | */ |
| 387 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 388 | @Override |
| 389 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 390 | Collection<Class<? extends IFloodlightService>> l = |
| 391 | new ArrayList<Class<? extends IFloodlightService>>(); |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 392 | l.add(IControllerRegistryService.class); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 393 | return l; |
| 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 398 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = |
| 399 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 400 | m.put(IControllerRegistryService.class, this); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 401 | return m; |
| 402 | } |
| 403 | |
| 404 | @Override |
| 405 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 406 | Collection<Class<? extends IFloodlightService>> l = |
| 407 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 408 | l.add(IRestApiService.class); |
| 409 | return l; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public void init (FloodlightModuleContext context) throws FloodlightModuleException { |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 414 | log.info("Initialising the Zookeeper Registry - Zookeeper connection required"); |
| 415 | |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 416 | //Read the Zookeeper connection string from the config |
| 417 | Map<String, String> configParams = context.getConfigParams(this); |
| 418 | String connectionString = configParams.get("connectionString"); |
| 419 | if (connectionString != null){ |
| 420 | this.connectionString = connectionString; |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 421 | } |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 422 | log.info("Setting Zookeeper connection string to {}", this.connectionString); |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 423 | |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 424 | restApi = context.getServiceImpl(IRestApiService.class); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 425 | |
| 426 | switchLatches = new HashMap<String, LeaderLatch>(); |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 427 | switchCallbacks = new HashMap<String, ControlChangeCallback>(); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 428 | switchPathCaches = new HashMap<String, PathChildrenCache>(); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 429 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 430 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 431 | client = CuratorFrameworkFactory.newClient(this.connectionString, |
Jonathan Hart | cc957a0 | 2013-02-26 10:39:04 -0800 | [diff] [blame] | 432 | sessionTimeout, connectionTimeout, retryPolicy); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 433 | |
| 434 | client.start(); |
| 435 | |
| 436 | client = client.usingNamespace(namespace); |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 437 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 438 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 439 | controllerCache = new PathChildrenCache(client, controllerPath, true); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 440 | switchCache = new PathChildrenCache(client, switchLatchesPath, true); |
| 441 | switchCache.getListenable().addListener(switchPathCacheListener); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 442 | |
| 443 | try { |
| 444 | controllerCache.start(StartMode.BUILD_INITIAL_CACHE); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 445 | |
| 446 | //Don't prime the cache, we want a notification for each child node in the path |
| 447 | switchCache.start(StartMode.NORMAL); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 448 | } catch (Exception e) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 449 | throw new FloodlightModuleException("Error initialising ZookeeperRegistry: " |
| 450 | + e.getMessage()); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 451 | } |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | @Override |
| 455 | public void startUp (FloodlightModuleContext context) { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 456 | restApi.addRestletRoutable(new RegistryWebRoutable()); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 457 | } |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 458 | } |