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