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