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