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