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